package app.fitness.fragment;

import iterface.ITaskCallBacks;

import java.util.Calendar;
import java.util.Date;

import singleton.SingleTon;
import utils.Utils;
import adapter.CircleFragmentAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import app.fitness.simplefragment.DashBoardCaloriesSimpleFragment;
import app.fitness.simplefragment.DashBoardDistanceSimpleFragment;
import app.fitness.simplefragment.DashBoardSleepSimpleFragment;
import app.fitness.simplefragment.DashBoardStepsSimpleFragment;

import com.razer.android.nabuopensdk.AuthCheckCallback;
import com.razer.android.nabuopensdk.NabuOpenSDK;
import com.razer.android.nabuopensdk.interfaces.NabuAuthListener;
import com.razerzone.android.fitness.ActivityFitness;
import com.razerzone.android.fitness.R;
import com.viewpagerindicator.CirclePageIndicator;

import controller.GetFitnessData;
import controller.GetSleepData;
import controller.UserProfileDataAsync;
import de.passsy.holocircularprogressbar.HoloCircularProgressBar;
import define.Constants;
import define.DashBoard;
import define.DashBoardDetail;
import define.FitnessData;
import define.FitnessHistoryData;
import define.Key;
import define.Language;
import define.Position;
import define.SleepData;

public class DashBoardFragment extends Fragment implements OnClickListener,
		OnTouchListener {
	/**
	 * Data section
	 */
	private CircleFragmentAdapter circleFragmentAdapter;

	/**
	 * Interface section
	 */
	public static ITaskCallBacks iTaskCallBacks;

	/**
	 * String section
	 */
	public static boolean IS_AUTHENTICATE = false;
	public static boolean IS_RESUME_FROM_BACKGROUND = false;
	public static boolean IS_GO_FROM_DASHBOARD_TO_GOAL_PAGE = false;
	private static boolean IS_FIRST_TIME_DOWNLOAD_DATA = true;
	public static boolean IS_FIRST_TIME_DOWNLOAD_DATA_TODAY_FITNESS = true;
	public static boolean IS_FIRST_TIME_DOWNLOAD_DATA_TODAY_SLEEP = true;
	public static boolean IS_FIRST_TIME_LOAD_DATA_FROM_SHARED_PREFERENCES = true;
	public static boolean IS_IN_DASHBOARD = true;

	 private boolean IS_SHOWING_LEFT_ARROW = false;
	 private boolean IS_SHOWING_RIGHT_ARROW = false;

	private static boolean IS_SIGNED_IN = false;

	// Yesterday & Tomorrow
	private int DAY = 0;
	private int TOMORROW = 0;
	private int YESTERDAY = 0;

	// 1 Month
	private int INDEX_MONTH = 0;
	private int MONTH = 0;
	private int NEXT_MONTH = 0;
	private int PREVIOUS_MONTH = 0;

	// 3 Months
	private int INDEX_MONTHS = 0;
	private int MONTHS = 0;
	private int NEXT_MONTHS = 0;
	private int PREVIOUS_MONTHS = 0;

	// Week
	private int INDEX_WEEK = 0;
	private int NEXT_WEEK = 0;
	private int PREVIOUS_WEEK = 0;
	private int WEEK = 0;

	// Year
	private int INDEX_YEAR = 0;
	private int NEXT_YEAR = 0;
	private int PREVIOUS_YEAR = 0;
	private int YEAR = 0;

	public static int CASE = DashBoard.STEPS;
	public final static String TAG = DashBoardFragment.class.getSimpleName();

	/**
	 * Data section
	 */
	private GetFitnessData getFitnessData;
	public static SingleTon singleTon = SingleTon.getInstance();

	/**
	 * View section
	 */
	private Button mBtnCancel;
	private CirclePageIndicator circlePageIndicator;
	public static HoloCircularProgressBar holoCircularPbActiveMin;
	public static HoloCircularProgressBar holoCircularPbCalories;
	public static HoloCircularProgressBar holoCircularPbDistance;
	// public static HoloCircularProgressBar holoCircularPbFloors;
	public static HoloCircularProgressBar holoCircularPbSleep;
	public static HoloCircularProgressBar holoCircularPbSteps;
	public static ImageButton mIbtnLeft;
	public static ImageButton mIbtnRight;
	private LinearLayout mLl1Month;
	private LinearLayout mLl3Months;
	// private LinearLayout mLlActiveMins;
	// private LinearLayout mLlCalories;
	// private LinearLayout mLlDistance;
	private LinearLayout mLlDaily;
	private LinearLayout mLlDashBoard;
	// private LinearLayout mLlSleep;
	// private LinearLayout mLlSteps;
	private LinearLayout mLlWeekly;
	private LinearLayout mLlYearly;
	public static PopupWindow mPw;
	private TextView mTv1Month;
	private TextView mTv3Months;
	// private TextView mTvActiveMinsContent;
	// private static TextView mTvActiveMinsTitle;
	private TextView mTvCalories;
	public static TextView mTvCaloriesContent;
	public static TextView mTvCaloriesTitle;
	private TextView mTvDaily;
	public static TextView mTvDate;
	private TextView mTvDistance;
	public static TextView mTvDistanceContent;
	public static TextView mTvDistanceTitle;
	// private TextView mTvFloorsContent;
	// public static TextView mTvFloorsTitle;
	private TextView mTvSleep;
	public static TextView mTvSleepHoursContent;
	public static TextView mTvSleepMinutesTitle;
	public static TextView mTvSleepHoursTitle;
	private TextView mTvSteps;
	public static TextView mTvStepsContent;
	public static TextView mTvStepsTitle;
	private TextView mTvWeekly;
	private TextView mTvYearly;
	public static ViewPager mViewPager;

	/**
	 * Listener
	 */
	private OnClickListener mListenerActionSheet = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// Enable all views in page
			enableAllViews();

			switch (v.getId()) {
			case R.id.btn_cancel_in_action_sheet_dashboard_detail:
				if (mPw != null && mPw.isShowing()) {
					mPw.dismiss();
				}
				break;
			case R.id.tv_1_month:
				resetDateIndex();

				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = Key.KEY_1_MONTH;
				singleTon
						.setCurrentTimePicker(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

				/**
				 * Reset goal value
				 */
				resetGoalValue();

				/**
				 * Get goal current value
				 */
				getMonth(false, 0);

				/**
				 * Check show Backward button or Forward button
				 */
				checkIndex1Month(0);

				// Hide Pop up Window
				Utils.hidePopUp(mPw);
				break;
			case R.id.tv_3_months:
				resetDateIndex();

				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = Key.KEY_3_MONTHS;
				singleTon
						.setCurrentTimePicker(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

				/**
				 * Reset goal value
				 */
				resetGoalValue();

				/**
				 * Get goal current value
				 */
				getMonths(false, 0);
				/**
				 * Check show Backward button or Forward button
				 */
				checkIndex3Months(0);

				// Hide Pop up Window
				Utils.hidePopUp(mPw);

				break;
			case R.id.tv_daily:
				resetDateIndex();

				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = Key.KEY_DAILY;
				singleTon
						.setCurrentTimePicker(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

				// Set current date
				mTvDate.setText(getString(R.string.today) + " "
						+ Utils.getCurrentDate(getActivity()).toUpperCase());
				singleTon.setCurrentDate(mTvDate.getText().toString());

				// Hide Pop up Window
				Utils.hidePopUp(mPw);

				/**
				 * Reset goal value
				 */
				resetGoalValue();

				/**
				 * Check show Backward button or Forward button
				 */
				checkIndexDaily();

				/**
				 * Check if fitness data is not empty, call it
				 */
				getDay(0);
				break;
			case R.id.tv_weekly:
				resetDateIndex();

				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = Key.KEY_WEEKLY;
				singleTon
						.setCurrentTimePicker(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

				/**
				 * Reset goal value
				 */
				resetGoalValue();

				/**
				 * Get current value
				 */
				getLastWeek(0);

				/**
				 * Check show Backward button or Forward button
				 */
				checkIndexWeek(WEEK);

				// Hide Pop up Window
				Utils.hidePopUp(mPw);

				break;
			case R.id.tv_yearly:
				resetDateIndex();

				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = Key.KEY_YEARLY;
				singleTon
						.setCurrentTimePicker(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

				/**
				 * Reset goal value
				 */
				resetGoalValue();

				/**
				 * Check show Backward button or Forward button
				 */
				checkIndexYear(0);

				/**
				 * Get current value
				 */
				getYear(0);

				// Hide Pop up Window
				Utils.hidePopUp(mPw);

				break;
			default:
				break;
			}

			/**
			 * Set background for time picker options
			 */
			setBackgroundDialog(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);
		}
	};

	private OnPageChangeListener mListenerPageSelected = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int position) {
			if (!IS_SIGNED_IN | DashBoardDetailFragment.IS_IN_DASHBOARD_DETAIL) {
				return;
			}

			switch (position) {
			case Position.SLEEP:
				/**
				 * Calculate again to get correct total sleep data
				 */
				calculateTotalSleep();

				updateSleepDataOnPage(getActivity(),
						singleTon.getTotalSleepHours(),
						singleTon.getTotalSleepMinutes(),
						singleTon.getDeepSleepHours(),
						singleTon.getDeepSleepMinutes(),
						singleTon.getLightSleepHours(),
						singleTon.getLightSleepMinutes(),
						singleTon.getSleepGoal());
				break;
			default:
				// Need refresh
				updateFitnessDataOnPage(getActivity(),
						singleTon.getCaloriesCurrent(),
						singleTon.getCaloriesGoal(),
						singleTon.getDistanceCurrent(),
						singleTon.getDistanceGoal(),
						singleTon.getStepsCurrent(), singleTon.getStepsGoal());
				break;
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	};

	/**
	 * Runnable
	 */

	/**
	 * The others
	 */
	private Calendar mCalendarGetData = Calendar.getInstance();
	public static Calendar mCalendarWeek = Calendar.getInstance();
	private SharedPreferences mSp;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		iTaskCallBacks = (ITaskCallBacks) activity;
	}

	// On click
	@Override
	public void onClick(View v) {
		Fragment mFm = new DashBoardDetailFragment();
		switch (v.getId()) {
		case R.id.circularprogressbar_active_mins_in_dash_board:
			// CASE = DashBoard.ACTIVE_MINS;

			// Show Active Minutes fragment in here
			// transferToDashBoardDetailPage(mFm);
			break;
		case R.id.circularprogressbar_calories_in_dash_board:
			CASE = DashBoard.CALORIES;

			// Show Calories fragment in here
			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.circularprogressbar_distance_in_dash_board:
			CASE = DashBoard.DISTANCE;

			// Show Distance fragment in here
			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.circularprogressbar_sleep_in_dash_board:
			CASE = DashBoard.SLEEP;

			// Show Sleep fragment in here
			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.circularprogressbar_steps_in_dash_board:
			CASE = DashBoard.STEPS;

			// Show Steps fragment in here
			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.ibtn_left_in_dashboard:
			if (mPw != null && mPw.isShowing()) {
				mPw.dismiss();
			}

			/**
			 * Show data for yesterday and before
			 */
			switch (DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD) {
			case Key.KEY_1_MONTH:
				PREVIOUS_MONTH++;
				MONTH = PREVIOUS_MONTH - NEXT_MONTH;

				// Step 1
				getMonth(false, MONTH);
				checkIndex1Month(MONTH);
				break;
			case Key.KEY_3_MONTHS:
				PREVIOUS_MONTHS++;
				MONTHS = PREVIOUS_MONTHS - NEXT_MONTHS;

				getMonths(false, MONTHS);
				checkIndex3Months(MONTHS);
				break;
			case Key.KEY_DAILY:
				YESTERDAY++;
				DAY = YESTERDAY - TOMORROW;

				checkIndexDaily();
				// Get yesterday
				getDay(DAY);
				break;
			case Key.KEY_WEEKLY:
				PREVIOUS_WEEK++;
				WEEK = PREVIOUS_WEEK - NEXT_WEEK;

				getLastWeek(WEEK);
				checkIndexWeek(WEEK);
				break;
			case Key.KEY_YEARLY:
				PREVIOUS_YEAR++;
				YEAR = PREVIOUS_YEAR - NEXT_YEAR;

				checkIndexYear(YEAR);
				getYear(YEAR);
				break;
			default:
				break;
			}
			break;
		case R.id.ibtn_right_in_dashboard:
			if (mPw != null && mPw.isShowing()) {
				mPw.dismiss();
			}

			/**
			 * Show data for yesterday and before
			 */
			switch (DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD) {
			case Key.KEY_1_MONTH:
				NEXT_MONTH++;
				MONTH = PREVIOUS_MONTH - NEXT_MONTH;

				getMonth(true, MONTH);
				checkIndex1Month(MONTH);
				break;
			case Key.KEY_3_MONTHS:
				NEXT_MONTHS++;
				MONTHS = PREVIOUS_MONTHS - NEXT_MONTHS;

				getMonths(true, MONTHS);
				checkIndex3Months(MONTHS);
				break;
			case Key.KEY_DAILY:
				TOMORROW++;
				DAY = YESTERDAY - TOMORROW;

				checkIndexDaily();

				// Get tomorrow
				getDay(DAY);

				break;
			case Key.KEY_WEEKLY:
				NEXT_WEEK++;
				WEEK = PREVIOUS_WEEK - NEXT_WEEK;

				getNextWeek(WEEK);
				checkIndexWeek(WEEK);

				break;
			case Key.KEY_YEARLY:
				NEXT_YEAR++;
				YEAR = PREVIOUS_YEAR - NEXT_YEAR;

				checkIndexYear(YEAR);

				getYear(YEAR);

				break;
			default:
				break;
			}
			break;
		case R.id.tv_calories_in_dash_board:
			CASE = DashBoard.CALORIES;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_calories_content_in_dash_board:
			CASE = DashBoard.CALORIES;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_calories_title_in_dash_board:
			CASE = DashBoard.CALORIES;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_distance_in_dash_board:
			CASE = DashBoard.DISTANCE;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_distance_content_in_dash_board:
			CASE = DashBoard.DISTANCE;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_distance_title_in_dash_board:
			CASE = DashBoard.DISTANCE;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_sleep_in_dash_board:
			CASE = DashBoard.SLEEP;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_sleep_hours_content_in_dash_board:
			CASE = DashBoard.SLEEP;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_sleep_hours_title_in_dash_board:
			CASE = DashBoard.SLEEP;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_sleep_minutes_title_in_dash_board:
			CASE = DashBoard.SLEEP;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_steps_in_dash_board:
			CASE = DashBoard.STEPS;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_steps_content_in_dash_board:
			CASE = DashBoard.STEPS;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_steps_title_in_dash_board:
			CASE = DashBoard.STEPS;

			transferToDashBoardDetailPage(mFm);
			break;
		case R.id.tv_date_in_dashboard:
			/**
			 * Show action sheet
			 */
			View view = getActivity().getLayoutInflater().inflate(
					R.layout.action_sheet_dashboard_detail, null, false);
			initialVariablesActionSheet(view);
			initialDataActionSheet();

			setBackgroundDialog(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

			if (mPw == null) {
				// Show pop up (action sheet) in here
				mPw = Utils.showActionSheetDialog(getActivity(), view);
			} else {
				if (!mPw.isShowing()) {
					// Show pop up (action sheet) in here
					mPw = Utils.showActionSheetDialog(getActivity(), view);
				}
			}

			// Disable all views in page
			disableAllViews();
			break;
		default:
			break;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Initialize fitness object to get data
		getFitnessData = new GetFitnessData(getActivity());

		if (IS_IN_DASHBOARD) {
			/**
			 * If recognized the Language System was changed, should set flag be
			 * true
			 */
			if (Constants.CURRENT_LANGUAGE_SYSTEM != Utils
					.checkLanguageInSettings()) {
				Constants.CURRENT_LANGUAGE_SYSTEM = Utils
						.checkLanguageInSettings();
				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = singleTon
						.getCurrentTimePicker();

				IS_RESUME_FROM_BACKGROUND = true;
			}

			if (IS_FIRST_TIME_DOWNLOAD_DATA & !IS_RESUME_FROM_BACKGROUND) {
				IS_FIRST_TIME_DOWNLOAD_DATA = false;
				/**
				 * Set current date in the first time
				 */
				singleTon.setCurrentTimePicker(Key.KEY_DAILY);
				singleTon.setCurrentDate(getString(R.string.today) + " "
						+ Utils.getCurrentDate(getActivity()).toUpperCase());

				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = singleTon
						.getCurrentTimePicker();
			}

			Constants.utils = new Utils(getActivity());
			if (!Constants.utils.isNetworkOnline()) {
				// Exit the application
				android.os.Process.killProcess(android.os.Process.myPid());
			} else {
				/**
				 * Get new data from the server in the first time run
				 * application, after that, stored it, if its data already had,
				 * no need get new again
				 */
				if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
					if (!IS_SIGNED_IN
							& !IS_RESUME_FROM_BACKGROUND
							& !DashBoardDetailFragment.IS_IN_DASHBOARD_DETAIL
							& !DashBoardDetailFragment.IS_GO_TO_DASHBOARD_FROM_DASHBOARD_DETAIL) {
						authenticate();
					}
				}
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (IS_SIGNED_IN) {
			/**
			 * Always download user profile to avoid lost user info
			 */
			DashBoard.mFmDashBoard = new DashBoardFragment();
			new UserProfileDataAsync(getActivity()).execute();
		}
		
		// Reset
		DashBoardDetail.mFmDashBoardDetail = null;
		DashBoardFragment.IS_IN_DASHBOARD = true;
		DashBoardDetailFragment.IS_IN_DASHBOARD_DETAIL = false;

		// Initial action bar
		initialActionBar();

		// Set background color for drawer item when was chose
		ActivityFitness.mLLAbout.setBackgroundColor(Color.TRANSPARENT);
		ActivityFitness.mLLDashBoard.setBackgroundColor(getResources()
				.getColor(R.color.green));
		ActivityFitness.mLLGoalSettings.setBackgroundColor(Color.TRANSPARENT);

		/**
		 * Set Orientation for page
		 */
		getActivity().setRequestedOrientation(
				ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

		View v = inflater.inflate(R.layout.fragment_dash_board, container,
				false);

		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			/**
			 * Landscape orientation
			 */
			// Hide action bar
			if (getActivity().getActionBar().isShowing()) {
				getActivity().getActionBar().hide();
			}

			circleFragmentAdapter = new CircleFragmentAdapter(
					getChildFragmentManager(),
					DashBoardStepsSimpleFragment.class);

			mViewPager = (ViewPager) v.findViewById(R.id.vp_dash_board);
			mViewPager.setPageMargin(42);
			mViewPager.setAdapter(circleFragmentAdapter);

			circlePageIndicator = (CirclePageIndicator) v
					.findViewById(R.id.circle_page_indicator_dash_board);
			circlePageIndicator.setViewPager(mViewPager);
			circlePageIndicator.setOnPageChangeListener(mListenerPageSelected);
		} else {
			/**
			 * Portrait orientation
			 */

			// Show action bar
			if (!getActivity().getActionBar().isShowing()) {
				getActivity().getActionBar().show();
			}
		}

		// Initialize variables
		initialVariables(v);
		initialData();

		/**
		 * Check signed in every 5 seconds to confirm that user already signed
		 * in or not
		 */
		if (!DashBoardDetailFragment.IS_IN_DASHBOARD_DETAIL
				| getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			/**
			 * Get data from server after already signed in
			 */
			if (DashBoardDetailFragment.IS_GO_TO_DASHBOARD_FROM_DASHBOARD_DETAIL) {
				savedInstanceState = null;

				/**
				 * Load data from Dash board Detail page in here
				 */
				// Update data on UI
				getAllData(savedInstanceState);
			} else {
				switch (DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD) {
				case Key.KEY_1_MONTH:
					break;
				case Key.KEY_3_MONTHS:
					break;
				case Key.KEY_DAILY:
					if (!IS_RESUME_FROM_BACKGROUND & IS_IN_DASHBOARD) {
						setTimerToGetDataAfterSignedIn(savedInstanceState);
					}
					break;
				case Key.KEY_WEEKLY:
					break;
				case Key.KEY_YEARLY:
					break;
				}
			}
		}

		if (Constants.utils != null) {
			// Hide soft key board
			Utils.hideSoftKeyboard(getActivity());
		}

		return v;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();

		if (IS_IN_DASHBOARD
				& !GoalSettingsFragment.IS_GO_TO_DASHBOARD_PAGE_FROM_GOAL_PAGE
				& !AboutFragment.IS_GO_TO_DASHBOARD_PAGE_FROM_ABOUT_PAGE) {
			/**
			 * Need save all things into singleton before transfer to the other
			 * pages
			 */
			saveAllDataIntoSingleTon();
		}
		if (mPw != null && mPw.isShowing()) {
			mPw.dismiss();
		}

		// Reset
		IS_AUTHENTICATE = false;
	}

	@Override
	public void onDetach() {
		super.onDetach();

		/**
		 * OnDetach will be not run if the current fragment already put into
		 * BackStack
		 */
		if (mPw != null && mPw.isShowing()) {
			mPw.dismiss();
		}

		iTaskCallBacks = null;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		if (!IS_SIGNED_IN | DashBoardDetailFragment.IS_IN_DASHBOARD_DETAIL) {
			return;
		}

		/**
		 * Calculate again to get correct total sleep data
		 */
		calculateTotalSleep();

		/**
		 * Activities current value
		 */
		// Put Activities information
		// outState.putString(Key.ACTIVE_MINS,
		// mTvActiveMinsTitleTitle.getText().toString());
		outState.putString(Key.CALORIES_CURRENT, singleTon.getCaloriesCurrent());
		outState.putString(Key.DISTANCE_CURRENT, singleTon.getDistanceCurrent());
		outState.putString(Key.SLEEP_HOURS_TITLE,
				singleTon.getTotalSleepHours());
		outState.putString(Key.SLEEP_MINUTE_TITLE,
				singleTon.getTotalSleepMinutes());
		outState.putString(Key.STEPS_CURRENT, singleTon.getStepsCurrent());

		/**
		 * Activities goal value
		 */
		outState.putString(Key.CALORIES_GOAL, singleTon.getCaloriesGoal());
		outState.putString(Key.DISTANCE_GOAL, singleTon.getDistanceGoal());
		outState.putString(Key.SLEEP_HOURS_CONTENT, singleTon.getSleepGoal());
		outState.putString(Key.STEPS_GOAL, singleTon.getStepsGoal());

		// Put current date
		outState.putString(Key.DATE, mTvDate.getText().toString());

		// Save index of day
		outState.putInt(Key.DAY, DAY);
		outState.putInt(Key.TOMORROW, TOMORROW);
		outState.putInt(Key.YESTERDAY, YESTERDAY);

		// Month
		outState.putInt(Key.INDEX_MONTH, INDEX_MONTH);
		outState.putInt(Key.MONTH, MONTH);
		outState.putInt(Key.NEXT_MONTH, NEXT_MONTH);
		outState.putInt(Key.PREVIOUS_MONTH, PREVIOUS_MONTH);

		// Months
		outState.putInt(Key.INDEX_MONTHS, INDEX_MONTHS);
		outState.putInt(Key.MONTHS, MONTHS);
		outState.putInt(Key.NEXT_MONTHS, NEXT_MONTHS);
		outState.putInt(Key.PREVIOUS_MONTHS, PREVIOUS_MONTHS);

		// Week
		outState.putInt(Key.INDEX_WEEK, INDEX_WEEK);
		outState.putInt(Key.NEXT_WEEK, NEXT_WEEK);
		outState.putInt(Key.PREVIOUS_WEEK, PREVIOUS_WEEK);
		outState.putInt(Key.WEEK, WEEK);

		// Year
		outState.putInt(Key.INDEX_YEAR, INDEX_YEAR);
		outState.putInt(Key.NEXT_YEAR, NEXT_YEAR);
		outState.putInt(Key.PREVIOUS_YEAR, PREVIOUS_YEAR);
		outState.putInt(Key.YEAR, YEAR);

		// Put Key of time picker
		outState.putInt(Key.CURRENT_TIME_PICKER,
				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

		// TODO Put flag value to check show Left Arrow and Right Arrow or not
		 if (mIbtnLeft != null & mIbtnRight != null) {
		 if (mIbtnLeft.isShown()) {
		 IS_SHOWING_LEFT_ARROW = true;
		 } else {
		 IS_SHOWING_LEFT_ARROW = false;
		 }
		 if (mIbtnRight.isShown()) {
		 IS_SHOWING_RIGHT_ARROW = true;
		 } else {
		 IS_SHOWING_RIGHT_ARROW = false;
		 }
		
		 outState.putBoolean(Key.IS_SHOWING_LEFT_ARROW,
		 IS_SHOWING_LEFT_ARROW);
		 outState.putBoolean(Key.IS_SHOWING_RIGHT_ARROW,
		 IS_SHOWING_RIGHT_ARROW);
		 }

		/**
		 * Need save all things into singleton before coming into Background and
		 * resume from background
		 */
		saveAllDataIntoSingleTon();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.ll_dash_board:
			if (mPw != null) {
				if (mPw.isShowing()) {
					/**
					 * Does not allow user can click any view on Dash board
					 * page, After that, dismiss option dialog
					 */
					mPw.dismiss();

					// Enable all views in page
					enableAllViews();
				}
			}
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onViewStateRestored(Bundle savedInstanceState) {
		super.onViewStateRestored(savedInstanceState);

		if (IS_IN_DASHBOARD) {
			if (DashBoardDetailFragment.IS_GO_TO_DASHBOARD_FROM_DASHBOARD_DETAIL) {
				savedInstanceState = null;
			}

			if (savedInstanceState != null) {
				if (DashBoardDetailFragment.IS_IN_DASHBOARD_DETAIL) {
					return;
				}

				/**
				 * Load avatar
				 */
				if (singleTon.getNickName() != null) {
					ActivityFitness.mTvNickNameBesideAvatar.setText(singleTon
							.getNickName());
				}
				if (singleTon.getBitmapAvatar() != null) {
					ActivityFitness.mIbtnAvatarInDrawer
							.setImageBitmap(singleTon.getBitmapAvatar());
				}

				/**
				 * Activities current value
				 */
				if (savedInstanceState.getString(Key.CALORIES_CURRENT) == null
						| savedInstanceState.getString(Key.DISTANCE_CURRENT) == null
						| savedInstanceState.getString(Key.STEPS_CURRENT) == null) {
					return;
				}

				mTvCaloriesTitle.setText(Utils
						.showIntergerFormat(savedInstanceState
								.getString(Key.CALORIES_CURRENT)));
				mTvDistanceTitle.setText(Utils.showIntergerFormat(Utils
						.showIntergerFormat(savedInstanceState
								.getString(Key.DISTANCE_CURRENT))));
				mTvStepsTitle.setText(Utils
						.showIntergerFormat(savedInstanceState
								.getString(Key.STEPS_CURRENT)));

				if (savedInstanceState.getString(Key.SLEEP_HOURS_TITLE) != null
						& savedInstanceState.getString(Key.SLEEP_MINUTE_TITLE) != null) {
					// Sleep current value
					int[] time = Utils.getCorrectHours(Integer
							.valueOf(savedInstanceState
									.getString(Key.SLEEP_HOURS_TITLE)), Integer
							.valueOf(savedInstanceState
									.getString(Key.SLEEP_MINUTE_TITLE)));

					mTvSleepHoursTitle.setText(Utils.showIntergerFormat(time[0]
							+ ""));
					mTvSleepMinutesTitle.setText(time[1] + "");
				}

				/**
				 * Activities goal value
				 */
				mTvCaloriesContent.setText(Utils
						.showIntergerFormat(savedInstanceState
								.getString(Key.CALORIES_GOAL)));
				mTvDistanceContent.setText(Utils
						.showIntergerFormat(savedInstanceState
								.getString(Key.DISTANCE_GOAL)));
				mTvSleepHoursContent.setText(savedInstanceState
						.getString(Key.SLEEP_HOURS_CONTENT) + "h");
				mTvStepsContent.setText(Utils
						.showIntergerFormat(savedInstanceState
								.getString(Key.STEPS_GOAL)));

				// Day
				DAY = savedInstanceState.getInt(Key.DAY);
				TOMORROW = savedInstanceState.getInt(Key.TOMORROW);
				YESTERDAY = savedInstanceState.getInt(Key.YESTERDAY);

				// Month
				INDEX_MONTH = savedInstanceState.getInt(Key.INDEX_MONTH);
				MONTH = savedInstanceState.getInt(Key.MONTH);
				NEXT_MONTH = savedInstanceState.getInt(Key.NEXT_MONTH);
				PREVIOUS_MONTH = savedInstanceState.getInt(Key.PREVIOUS_MONTH);

				// Months
				INDEX_MONTHS = savedInstanceState.getInt(Key.INDEX_MONTHS);
				MONTHS = savedInstanceState.getInt(Key.MONTHS);
				NEXT_MONTHS = savedInstanceState.getInt(Key.NEXT_MONTHS);
				PREVIOUS_MONTHS = savedInstanceState
						.getInt(Key.PREVIOUS_MONTHS);

				// Week
				INDEX_WEEK = savedInstanceState.getInt(Key.INDEX_WEEK);
				NEXT_WEEK = savedInstanceState.getInt(Key.NEXT_WEEK);
				PREVIOUS_WEEK = savedInstanceState.getInt(Key.PREVIOUS_WEEK);
				WEEK = savedInstanceState.getInt(Key.WEEK);

				// Year
				INDEX_YEAR = savedInstanceState.getInt(Key.INDEX_YEAR);
				NEXT_YEAR = savedInstanceState.getInt(Key.NEXT_YEAR);
				PREVIOUS_YEAR = savedInstanceState.getInt(Key.PREVIOUS_YEAR);
				YEAR = savedInstanceState.getInt(Key.YEAR);

				// Arrow
				IS_SHOWING_LEFT_ARROW = savedInstanceState
						.getBoolean(Key.IS_SHOWING_LEFT_ARROW);
				IS_SHOWING_RIGHT_ARROW = savedInstanceState
						.getBoolean(Key.IS_SHOWING_RIGHT_ARROW);

				/**
				 * Set data
				 */
				// Set current time picker
				DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = savedInstanceState
						.getInt(Key.CURRENT_TIME_PICKER);

				if (!IS_RESUME_FROM_BACKGROUND) {
					// Set date
					mTvDate.setText(savedInstanceState.getString(Key.DATE));
					DashBoardFragment.singleTon.setCurrentDate(mTvDate
							.getText().toString());
				} else {
					switch (DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD) {
					case Key.KEY_1_MONTH:
						getMonth(false, singleTon.getMonth());
						checkIndex1Month(singleTon.getMonth());
						break;
					case Key.KEY_3_MONTHS:
						getMonths(false, singleTon.getMonths());
						checkIndex3Months(singleTon.getMonths());
						break;
					case Key.KEY_DAILY:
						getDay(singleTon.getDay());
						checkIndexDaily();
						break;
					case Key.KEY_WEEKLY:
						getLastWeek(singleTon.getWeek());
						checkIndexWeek(singleTon.getWeek());
						break;
					case Key.KEY_YEARLY:
						checkIndexYear(singleTon.getYear());
						getYear(singleTon.getYear());
						break;
					}

					IS_RESUME_FROM_BACKGROUND = false;
				}

				/**
				 *Check show Backward arrow
				 * or Forward arrow in here
				 */
				if (mIbtnLeft != null & mIbtnRight != null) {
					// Show button correctly
					if (IS_SHOWING_LEFT_ARROW) {
						mIbtnLeft.setVisibility(View.VISIBLE);
					} else {
						mIbtnLeft.setVisibility(View.INVISIBLE);
					}
					if (IS_SHOWING_RIGHT_ARROW) {
						mIbtnRight.setVisibility(View.VISIBLE);
					} else {
						mIbtnRight.setVisibility(View.INVISIBLE);
					}
				}
			}
		}
	}

	/**
	 * Initialize methods
	 */

	private void initialActionBar() {
		// Show Drawer button
		getActivity().getActionBar().setDisplayShowHomeEnabled(true);

		// Show action bar also
		getActivity().getActionBar().show();

		// Show Back button
		ActivityFitness.mLLLeftActionBar.setVisibility(View.GONE);

		// Set title
		ActivityFitness.mTvTitle.setText(getString(R.string.dashboard_));

		// Show title
		ActivityFitness.mTvTitle.setVisibility(View.VISIBLE);

		/**
		 * Set Title Page is center
		 */
		switch (Utils.checkLanguageInSettings()) {
		case Language.CHINESE:
			ActivityFitness.mIvActionBar.setLayoutParams(new LayoutParams(Utils
					.getWidth(getActivity()) / 4,
					android.view.ViewGroup.LayoutParams.MATCH_PARENT));
			break;
		case Language.ENGLISH:
			ActivityFitness.mIvActionBar.setLayoutParams(new LayoutParams(Utils
					.getWidth(getActivity()) / 6 - 10,
					android.view.ViewGroup.LayoutParams.MATCH_PARENT));
			break;
		}
	}

	private void initialData() {
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// Set listener
			holoCircularPbActiveMin.setOnClickListener(this);
			holoCircularPbCalories.setOnClickListener(this);
			holoCircularPbDistance.setOnClickListener(this);
			// holoCircularPbFloors.setOnClickListener(this);
			holoCircularPbSleep.setOnClickListener(this);
			holoCircularPbSteps.setOnClickListener(this);

			mLlDashBoard.setOnTouchListener(this);

			mTvCalories.setOnClickListener(this);
			mTvCaloriesContent.setOnClickListener(this);
			mTvCaloriesTitle.setOnClickListener(this);
			mTvDistance.setOnClickListener(this);
			mTvDistanceContent.setOnClickListener(this);
			mTvDistanceTitle.setOnClickListener(this);
			mTvSleep.setOnClickListener(this);
			mTvSleepHoursContent.setOnClickListener(this);
			mTvSleepHoursTitle.setOnClickListener(this);
			mTvSleepMinutesTitle.setOnClickListener(this);
			mTvSteps.setOnClickListener(this);
			mTvStepsContent.setOnClickListener(this);
			mTvStepsTitle.setOnClickListener(this);
		}

		/**
		 * Set listener
		 */
		mIbtnLeft.setOnClickListener(this);
		mIbtnRight.setOnClickListener(this);
		mTvDate.setOnClickListener(this);
	}

	private void initialVariables(View v) {
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			holoCircularPbActiveMin = (HoloCircularProgressBar) v
					.findViewById(R.id.circularprogressbar_active_mins_in_dash_board);
			holoCircularPbCalories = (HoloCircularProgressBar) v
					.findViewById(R.id.circularprogressbar_calories_in_dash_board);
			holoCircularPbDistance = (HoloCircularProgressBar) v
					.findViewById(R.id.circularprogressbar_distance_in_dash_board);
			holoCircularPbSleep = (HoloCircularProgressBar) v
					.findViewById(R.id.circularprogressbar_sleep_in_dash_board);
			holoCircularPbSteps = (HoloCircularProgressBar) v
					.findViewById(R.id.circularprogressbar_steps_in_dash_board);

			mLlDashBoard = (LinearLayout) v.findViewById(R.id.ll_dash_board);

			// mTvActiveMinsContent = (TextView) v
			// .findViewById(R.id.tv_active_mins_content_in_dash_board);
			// mTvActiveMinsTitle = (TextView) v
			// .findViewById(R.id.tv_active_mins_title_in_dash_board);
			mTvCalories = (TextView) v
					.findViewById(R.id.tv_calories_in_dash_board);
			mTvCaloriesContent = (TextView) v
					.findViewById(R.id.tv_calories_content_in_dash_board);
			mTvCaloriesTitle = (TextView) v
					.findViewById(R.id.tv_calories_title_in_dash_board);
			mTvDistance = (TextView) v
					.findViewById(R.id.tv_distance_in_dash_board);
			mTvDistanceContent = (TextView) v
					.findViewById(R.id.tv_distance_content_in_dash_board);
			mTvDistanceTitle = (TextView) v
					.findViewById(R.id.tv_distance_title_in_dash_board);
			mTvSleep = (TextView) v.findViewById(R.id.tv_sleep_in_dash_board);
			mTvSleepHoursContent = (TextView) v
					.findViewById(R.id.tv_sleep_hours_content_in_dash_board);
			mTvSleepHoursTitle = (TextView) v
					.findViewById(R.id.tv_sleep_hours_title_in_dash_board);
			mTvSleepMinutesTitle = (TextView) v
					.findViewById(R.id.tv_sleep_minutes_title_in_dash_board);
			mTvSteps = (TextView) v.findViewById(R.id.tv_steps_in_dash_board);
			mTvStepsContent = (TextView) v
					.findViewById(R.id.tv_steps_content_in_dash_board);
			mTvStepsTitle = (TextView) v
					.findViewById(R.id.tv_steps_title_in_dash_board);
		}

		mIbtnLeft = (ImageButton) v.findViewById(R.id.ibtn_left_in_dashboard);
		mIbtnRight = (ImageButton) v.findViewById(R.id.ibtn_right_in_dashboard);
		mTvDate = (TextView) v.findViewById(R.id.tv_date_in_dashboard);
	}

	private void initialDataActionSheet() {
		mBtnCancel.setOnClickListener(mListenerActionSheet);
		mTv1Month.setOnClickListener(mListenerActionSheet);
		mTv3Months.setOnClickListener(mListenerActionSheet);
		mTvDaily.setOnClickListener(mListenerActionSheet);
		mTvWeekly.setOnClickListener(mListenerActionSheet);
		mTvYearly.setOnClickListener(mListenerActionSheet);
	}

	private void initialVariablesActionSheet(View v) {
		mBtnCancel = (Button) v
				.findViewById(R.id.btn_cancel_in_action_sheet_dashboard_detail);
		mLl1Month = (LinearLayout) v
				.findViewById(R.id.ll_1_month_in_action_sheet);
		mLl3Months = (LinearLayout) v
				.findViewById(R.id.ll_3_months_in_action_sheet);
		mLlDaily = (LinearLayout) v.findViewById(R.id.ll_daily_in_action_sheet);
		mLlWeekly = (LinearLayout) v
				.findViewById(R.id.ll_weekly_in_action_sheet);
		mLlYearly = (LinearLayout) v
				.findViewById(R.id.ll_yearly_in_action_sheet);
		mTv1Month = (TextView) v.findViewById(R.id.tv_1_month);
		mTv3Months = (TextView) v.findViewById(R.id.tv_3_months);
		mTvDaily = (TextView) v.findViewById(R.id.tv_daily);
		mTvWeekly = (TextView) v.findViewById(R.id.tv_weekly);
		mTvYearly = (TextView) v.findViewById(R.id.tv_yearly);
	}

	/**
	 * Basic methods
	 */

	private void authenticate() {
		// Set boolean to tell that need to show progress dialog
		// before already got data from server
		IS_AUTHENTICATE = true;

		// Begin show dialog
		if (iTaskCallBacks != null) {
			iTaskCallBacks.onBeginReceiveData();
		}

		/**
		 * Begin sign in account & get data from server
		 */
		if (Constants.nabuSDK == null) {
			Constants.nabuSDK = NabuOpenSDK.getInstance(getActivity());
		}

		Constants.nabuSDK.initiate(getActivity(), Constants.APP_ID,
				Constants.SCOPE, new NabuAuthListener() {

					@Override
					public void onAuthSuccess(String arg0) {
						Log.i("onAuthSuccess", arg0);

						Constants.nabuSDK.checkAppAuthorized(getActivity(),
								new AuthCheckCallback() {

									@Override
									public void onSuccess(boolean result) {
										IS_SIGNED_IN = true;
										
										/**
										 * Always download user profile to avoid lost user info
										 */
										DashBoard.mFmDashBoard = new DashBoardFragment();
										new UserProfileDataAsync(getActivity()).execute();
									}

									@Override
									public void onFailed(String result) {
										/**
										 * Deleted cache before exit
										 */
										Utils.clearApplicationData(getActivity());

										try {
											Thread.sleep(2000);
										} catch (InterruptedException e) {
											e.printStackTrace();
										}

										android.os.Process
												.killProcess(android.os.Process
														.myPid());
									}
								});
					}

					@Override
					public void onAuthFailed(String error) {
						Log.i("onAuthFailed", error);

						/**
						 * If get invalid token, need terminate the application
						 */
						if (error.equals(Constants.INVALID_TOKEN)) {
							/**
							 * Deleted cache before exit
							 */
							Utils.clearApplicationData(getActivity());

							try {
								Thread.sleep(2000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}

							android.os.Process.killProcess(android.os.Process
									.myPid());
						} else if (error.equals("AUTHORIZATION FAILED")) {
							android.os.Process.killProcess(android.os.Process
									.myPid());
						}
					}
				});
	}

	private int calculateOffsetInMonth(int offset_from_today, int today,
			int first_day_of_picker_month_in_year, int current_month,
			int picker_month, int picker_year, int current_year) {
		if (picker_year < current_year) {
			// need to add offset of today
			Calendar mCalendarLastYear = Calendar.getInstance();
			mCalendarLastYear.set(Calendar.MONTH, Calendar.DECEMBER);
			mCalendarLastYear.set(Calendar.YEAR, picker_year);
			mCalendarLastYear.set(Calendar.DATE, 31);

			int last_day_of_picker_month = mCalendarLastYear
					.get(Calendar.DAY_OF_YEAR);

			/**
			 * If current month of current year is same as month of last year,
			 * calculate from today in current year to today of last year
			 */
			if (current_month == picker_month) {
				first_day_of_picker_month_in_year = today;
			}

			// Calculate from to day to picker day
			offset_from_today = today
					- 1
					+ (last_day_of_picker_month - first_day_of_picker_month_in_year);
		} else
			offset_from_today = today - first_day_of_picker_month_in_year;

		return offset_from_today;
	}

	private int calculateOffsetInMonths(int offset_from_today, int today,
			int first_day_of_picker_month_in_year, int picker_year,
			int current_year) {
		if (picker_year < current_year) {
			// need to add offset of today
			Calendar mCalendarLastYear = Calendar.getInstance();
			mCalendarLastYear.set(Calendar.MONTH, Calendar.DECEMBER);
			mCalendarLastYear.set(Calendar.YEAR, picker_year);
			mCalendarLastYear.set(Calendar.DATE, 31);

			int last_day_of_picker_year = mCalendarLastYear
					.get(Calendar.DAY_OF_YEAR);
			offset_from_today = today
					+ (last_day_of_picker_year - first_day_of_picker_month_in_year);
		} else {
			offset_from_today = today - first_day_of_picker_month_in_year;
		}

		return offset_from_today;
	}

	private void calculateTotalSleep() {
		int sleep_hours = Integer.valueOf(singleTon.getDeepSleepHours())
				+ Integer.valueOf(singleTon.getLightSleepHours());
		int sleep_minutes = Integer.valueOf(singleTon.getDeepSleepMinutes())
				+ Integer.valueOf(singleTon.getLightSleepMinutes());

		singleTon.setTotalSleepHours("" + sleep_hours);
		singleTon.setTotalSleepMinutes("" + sleep_minutes);
	}

	private void checkIndex1Month(int MONTH) {
		Calendar mCalendarToday = Calendar.getInstance();
		int today = mCalendarToday.get(Calendar.DAY_OF_MONTH);

		Calendar mCalendar = Calendar.getInstance();
		mCalendar
				.set(Calendar.MONTH, DashBoardFragment.singleTon
						.getCurrentMonthIndex(getActivity()));
		if (today != 1) {
			if (DashBoardFragment.getDataLength() < today) {
				mIbtnLeft.setVisibility(View.INVISIBLE);
				mIbtnRight.setVisibility(View.INVISIBLE);

				return;
			}
		}

		if (MONTH == 0) {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);
		} else if (DashBoardFragment.singleTon.getOffsetMonth() >= DashBoardFragment
				.getDataLength()) {
			mIbtnLeft.setVisibility(View.INVISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		} else {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		}
	}

	private void checkIndex3Months(int MONTHS) {
		Calendar mCalendarToday = Calendar.getInstance();
		int today = mCalendarToday.get(Calendar.DAY_OF_MONTH);
		if (DashBoardFragment.getDataLength() < today + 60) {
			mIbtnLeft.setVisibility(View.INVISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);
			return;
		}

		if (MONTHS == 0) {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);
		} else if (DashBoardFragment.singleTon.getOffsetMonths() > DashBoardFragment
				.getDataLength()) {
			mIbtnLeft.setVisibility(View.INVISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		} else {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		}
	}

	private void checkIndexDaily() {
		if (DAY == 0) {
			// If == 0
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);
		} else {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		}
	}

	private void checkIndexWeek(int WEEK) {
		if (DashBoardFragment.getDataLength() < 7) {
			mIbtnLeft.setVisibility(View.INVISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);

			return;
		}

		if (WEEK == 0) {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);
		} else if (DashBoardFragment.singleTon.getOffsetWeek() > DashBoardFragment
				.getDataLength()) {
			mIbtnLeft.setVisibility(View.INVISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		} else {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		}
	}

	private void checkIndexYear(int YEAR) {
		if (DashBoardFragment.getDataLength() < 365) {
			mIbtnLeft.setVisibility(View.INVISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);

			return;
		}

		if (YEAR == 0) {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.INVISIBLE);
		} else if (DashBoardFragment.singleTon.getOffsetYear() >= DashBoardFragment
				.getDataLength()) {
			mIbtnLeft.setVisibility(View.INVISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		} else {
			mIbtnLeft.setVisibility(View.VISIBLE);
			mIbtnRight.setVisibility(View.VISIBLE);
		}
	}

	private static void clearOldData() {
		FitnessHistoryData.mLlFhdCalories.clear();
		FitnessHistoryData.mLlFhdDistance.clear();
		FitnessHistoryData.mLlFhdSteps.clear();

		for (int i = 0; i < FitnessData.MONTH_CALORIES.length; i++) {
			FitnessData.MONTH_CALORIES[i] = 0;
			FitnessData.MONTH_DISTANCE[i] = 0;
			FitnessData.MONTH_STEPS[i] = 0;
		}
		for (int i = 0; i < FitnessData.MONTHS_CALORIES.length; i++) {
			FitnessData.MONTHS_CALORIES[i] = 0;
			FitnessData.MONTHS_DISTANCE[i] = 0;
			FitnessData.MONTHS_STEPS[i] = 0;
		}
		for (int i = 0; i < FitnessData.WEEK_CALORIES.length; i++) {
			FitnessData.WEEK_CALORIES[i] = 0;
			FitnessData.WEEK_DISTANCE[i] = 0;
			FitnessData.WEEK_STEPS[i] = 0;
		}
		for (int i = 0; i < FitnessData.YEAR_CALORIES.length; i++) {
			FitnessData.YEAR_CALORIES[i] = 0;
			FitnessData.YEAR_DISTANCE[i] = 0;
			FitnessData.YEAR_STEPS[i] = 0;
		}

		// Deep Sleep
		for (int i = 0; i < SleepData.MONTH_DEEP_SLEEP.length; i++) {
			SleepData.MONTH_DEEP_SLEEP[i] = 0;
		}
		for (int i = 0; i < SleepData.MONTHS_DEEP_SLEEP.length; i++) {
			SleepData.MONTHS_DEEP_SLEEP[i] = 0;
		}
		for (int i = 0; i < SleepData.WEEK_DEEP_SLEEP.length; i++) {
			SleepData.WEEK_DEEP_SLEEP[i] = 0;
		}
		for (int i = 0; i < SleepData.YEAR_DEEP_SLEEP.length; i++) {
			SleepData.YEAR_DEEP_SLEEP[i] = 0;
		}

		// Light Sleep
		for (int i = 0; i < SleepData.MONTH_LIGHT_SLEEP.length; i++) {
			SleepData.MONTH_LIGHT_SLEEP[i] = 0;
		}
		for (int i = 0; i < SleepData.MONTHS_LIGHT_SLEEP.length; i++) {
			SleepData.MONTHS_LIGHT_SLEEP[i] = 0;
		}
		for (int i = 0; i < SleepData.WEEK_LIGHT_SLEEP.length; i++) {
			SleepData.WEEK_LIGHT_SLEEP[i] = 0;
		}
		for (int i = 0; i < SleepData.YEAR_LIGHT_SLEEP.length; i++) {
			SleepData.YEAR_LIGHT_SLEEP[i] = 0;
		}
	}

	private void disableAllViews() {
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			/**
			 * disable All Views when pop up is showing
			 */
			mTvCalories.setEnabled(false);
			mTvCaloriesContent.setEnabled(false);
			mTvCaloriesTitle.setEnabled(false);

			mTvDistance.setEnabled(false);
			mTvDistanceContent.setEnabled(false);
			mTvDistanceTitle.setEnabled(false);

			mTvSteps.setEnabled(false);
			mTvStepsContent.setEnabled(false);
			mTvStepsTitle.setEnabled(false);

			mTvSleep.setEnabled(false);
			mTvSleepHoursContent.setEnabled(false);
			mTvSleepHoursTitle.setEnabled(false);
			mTvSleepMinutesTitle.setEnabled(false);
		}
	}

	private void enableAllViews() {
		/**
		 * Enable All Views when pop up is not showing
		 */
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			/**
			 * disable All Views when pop up is showing
			 */
			mTvCalories.setEnabled(true);
			mTvCaloriesContent.setEnabled(true);
			mTvCaloriesTitle.setEnabled(true);

			mTvDistance.setEnabled(true);
			mTvDistanceContent.setEnabled(true);
			mTvDistanceTitle.setEnabled(true);

			mTvSteps.setEnabled(true);
			mTvStepsContent.setEnabled(true);
			mTvStepsTitle.setEnabled(true);

			mTvSleep.setEnabled(true);
			mTvSleepHoursContent.setEnabled(true);
			mTvSleepHoursTitle.setEnabled(true);
			mTvSleepMinutesTitle.setEnabled(true);
		}
	}

	private void getAllData(Bundle savedInstanceState) {
		/**
		 * Check data, in case : When Back from Dash board Detail page. When
		 * change orientation
		 */
		if (savedInstanceState == null) {
			/**
			 * Show data in Portrait mode in the first time
			 */
			showDataOnPage();
		} else {
			/**
			 * Show data in Landscape mode after changed orientation
			 */
			setFitnessData(savedInstanceState);
		}
	}

	public static int getDataLength() {
		int index = 0;
		for (model.FitnessData item : FitnessHistoryData.fitness_history_data) {
			if (item != null) {
				if (item.getLocalDate() != null) {
					index++;
				}
			}
		}

		return index;
	}

	private void getDataValueFromDashBoardDetailPage() {
		// Day
		DAY = singleTon.getDay();
		YESTERDAY = singleTon.getYesterday();
		TOMORROW = singleTon.getTomorrow();

		// 1 Month
		INDEX_MONTH = singleTon.getIndexMonth();
		MONTH = singleTon.getMonth();
		NEXT_MONTH = singleTon.getNextMonth();
		PREVIOUS_MONTH = singleTon.getPreviousMonth();

		// 3 Months
		INDEX_MONTHS = singleTon.getIndexMonths();
		MONTHS = singleTon.getMonths();
		NEXT_MONTHS = singleTon.getNextMonths();
		PREVIOUS_MONTHS = singleTon.getPreviousMonths();

		// Week
		INDEX_WEEK = singleTon.getIndexWeek();
		WEEK = singleTon.getWeek();
		NEXT_WEEK = singleTon.getNextWeek();
		PREVIOUS_WEEK = singleTon.getPreviousWeek();

		// Year
		INDEX_YEAR = singleTon.getIndexYear();
		YEAR = singleTon.getYear();
		NEXT_YEAR = singleTon.getNextYear();
		PREVIOUS_YEAR = singleTon.getPreviousYear();

		// Set date
		mTvDate.setText(singleTon.getCurrentDate());
		DashBoardFragment.singleTon
				.setCurrentDate(mTvDate.getText().toString());

		DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = singleTon
				.getCurrentTimePicker();
		// Get current time picker
		switch (DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD) {
		case Key.KEY_1_MONTH:
			checkIndex1Month(MONTH);
			break;
		case Key.KEY_3_MONTHS:
			checkIndex3Months(MONTHS);
			break;
		case Key.KEY_DAILY:
			checkIndexDaily();
			break;
		case Key.KEY_WEEKLY:
			checkIndexWeek(WEEK);
			break;
		case Key.KEY_YEARLY:
			checkIndexYear(YEAR);
			break;
		}

		/**
		 * Update UI
		 */
		// Need refresh
		updateFitnessDataOnPage(getActivity(),
				DashBoardFragment.singleTon.getCaloriesCurrent(),
				DashBoardFragment.singleTon.getCaloriesGoal(),
				DashBoardFragment.singleTon.getDistanceCurrent(),
				DashBoardFragment.singleTon.getDistanceGoal(),
				DashBoardFragment.singleTon.getStepsCurrent(),
				DashBoardFragment.singleTon.getStepsGoal());

		/**
		 * Calculate again to get correct total sleep data
		 */
		calculateTotalSleep();
		updateSleepDataOnPage(getActivity(),
				DashBoardFragment.singleTon.getTotalSleepHours(),
				DashBoardFragment.singleTon.getTotalSleepMinutes(),
				DashBoardFragment.singleTon.getDeepSleepHours(),
				DashBoardFragment.singleTon.getDeepSleepMinutes(),
				DashBoardFragment.singleTon.getLightSleepHours(),
				DashBoardFragment.singleTon.getLightSleepMinutes(),
				DashBoardFragment.singleTon.getSleepGoal());

	}

	private void getDay(int DAY) {
		if (DAY == 0) {
			// Set text for day
			mTvDate.setText(getString(R.string.today) + " "
					+ Utils.getCurrentDate(getActivity()).toUpperCase() + "");
			singleTon.setCurrentDate(mTvDate.getText().toString());

			getFitnessDataInDays(DAY);
		} else if (DAY > 0) {
			String DATE = Utils.getLastDate(getActivity(), DAY).toUpperCase()
					+ "";
			// Set text for day
			mTvDate.setText(DATE);
			singleTon.setCurrentDate(mTvDate.getText().toString());

			getFitnessDataInDays(DAY);
		}

		/**
		 * Save goal values into single ton
		 */
		long sleep_goal = Long.parseLong(DashBoardFragment.singleTon
				.getSleepGoalDefault());

		/**
		 * Save activities goal value into singleton
		 */
		saveSleepGoalValueIntoSingleTon(sleep_goal + "");
	}

	private void getFitnessDataAllDaysInYear(int days) {
		// Define this fragment to update value on this page
		DashBoard.mFmDashBoard = new DashBoardFragment();

		// Call new thread to get Fitness History Data of
		// daily
		getFitnessData.getData(Key.FITNESS_HISTORY_DATA, 365);

		/**
		 * Download Sleep data for year
		 */
		GetSleepData.clearOldData();
		GetSleepData.getSleepData(getActivity(), Key.SLEEP_HISTORY_DATA);
	}

	private void getFitnessDataInDays(int days) {
		/**
		 * Show loading dialog
		 */
		if (iTaskCallBacks != null) {
			iTaskCallBacks.onBeginReceiveData();
		}

		GetFitnessData.clearOldData();

		// Define this fragment to update value on this page
		DashBoard.mFmDashBoard = new DashBoardFragment();

		mCalendarGetData.set(Calendar.HOUR_OF_DAY, 0);
		mCalendarGetData.set(Calendar.MINUTE, 0);
		mCalendarGetData.set(Calendar.SECOND, 0);

		// 1 day = 86400000
		long now = mCalendarGetData.getTimeInMillis() / 1000;
		long day = now - (days * 86400);

		/**
		 * Get Activities data for daily
		 */
		getFitnessData.getData(Key.FITNESS_DATA, (int) day);

		/**
		 * Get Sleep data for Daily
		 */
		GetSleepData.clearOldData();
		GetSleepData.getSleepData(getActivity(), Key.SLEEP_DATA);
	}

	private void getLastWeek(int WEEK) {
		if (WEEK == 0) {
			mTvDate.setText(Utils.getCurrentWeek(getActivity(),
					DashBoardFragment.mCalendarWeek));
		} else {
			if (DashBoardFragment.IS_RESUME_FROM_BACKGROUND) {
				mTvDate.setText(Utils.getLastWeek(getActivity(),
						DashBoardFragment.mCalendarWeek, -6, 6));
			} else {
				mTvDate.setText(Utils.getLastWeek(getActivity(),
						DashBoardFragment.mCalendarWeek, -13, 6));
			}
		}

		DashBoardFragment.singleTon
				.setCurrentDate(mTvDate.getText().toString());
		sumDataWeek();

		/**
		 * Save days index into single ton way to check show backward or forward
		 * button or not
		 */
		if (!DashBoardFragment.IS_RESUME_FROM_BACKGROUND) {
			if (WEEK == 0) {
				DashBoardFragment.singleTon.setOffsetWeek(7);
				INDEX_WEEK = 7;
			} else {
				DashBoardFragment.singleTon.setOffsetWeek(INDEX_WEEK + 7);
				INDEX_WEEK = DashBoardFragment.singleTon.getOffsetWeek();
			}

			DashBoardFragment.singleTon.setIndexWeek(INDEX_WEEK);
		}
	}

	private void getMonth(boolean left_right, int MONTH) {
		// Get month data
		Calendar mCalendarDate = null;
		if (MONTH == 0) {
			mCalendarDate = Utils.getCurrentMonth();
		} else {
			mCalendarDate = Utils.getLast1Month(MONTH);
		}

		// Set text for day
		String DATE = Utils.getCorrectMonth(getActivity(), mCalendarDate, -1)
				+ " " + mCalendarDate.get(Calendar.YEAR);
		mTvDate.setText(DATE);
		DashBoardFragment.singleTon
				.setCurrentDate(mTvDate.getText().toString());

		// Step 1 : Set single ton for month name and year name
		setCurrentDateInMonth(mCalendarDate);

		// Step 2 : Get month name, year name
		sumDataMonth(DashBoardFragment.singleTon
				.getCurrentMonthIndex(getActivity()));

		/**
		 * Check index to show Forward or Backward
		 */
		Calendar mCalendar = Calendar.getInstance();
		int today = mCalendar.get(Calendar.DAY_OF_MONTH);

		if (!IS_RESUME_FROM_BACKGROUND) {
			mCalendar.set(Calendar.MONTH, DashBoardFragment.singleTon
					.getCurrentMonthIndex(getActivity()));
			if (MONTH == 0) {
				DashBoardFragment.singleTon.setOffsetMonth(today);
				INDEX_MONTH = today;
			} else {

				if (!left_right) {
					DashBoardFragment.singleTon
							.setOffsetMonth(INDEX_MONTH
									+ mCalendar
											.getActualMaximum(Calendar.DAY_OF_MONTH));
				} else {
					DashBoardFragment.singleTon
							.setOffsetMonth(INDEX_MONTH
									- mCalendar
											.getActualMaximum(Calendar.DAY_OF_MONTH));
				}

				INDEX_MONTH = DashBoardFragment.singleTon.getOffsetMonth();
			}

			DashBoardFragment.singleTon.setIndexMonth(INDEX_MONTH);
		}
	}

	private void getMonths(boolean left_right, int MONTHS) {
		String DATE = null;
		// Set text
		Calendar mCalendarFirstMonth = Utils.getLast3MonthsFirst(getActivity(), MONTHS + 1);
		Calendar mCalendarLastMonth = Utils.getLast3MonthsLast(getActivity(), MONTHS);
		if (mCalendarFirstMonth.get(Calendar.YEAR) == mCalendarLastMonth
				.get(Calendar.YEAR)) {
			DATE = Utils
					.getCorrectMonth(getActivity(), mCalendarFirstMonth, -1)
					+ " - "
					+ Utils.getCorrectMonth(getActivity(), mCalendarLastMonth,
							-1) + " " + mCalendarFirstMonth.get(Calendar.YEAR);
		} else {
			DATE = Utils
					.getCorrectMonth(getActivity(), mCalendarFirstMonth, -1)
					+ " "
					+ mCalendarFirstMonth.get(Calendar.YEAR)
					+ " - "
					+ Utils.getCorrectMonth(getActivity(), mCalendarLastMonth,
							-1) + " " + mCalendarLastMonth.get(Calendar.YEAR);
		}

		mTvDate.setText(DATE);
		DashBoardFragment.singleTon
				.setCurrentDate(mTvDate.getText().toString());

		setCurrentDateInMonths(mCalendarFirstMonth, mCalendarLastMonth);

		/**
		 * Check already saved data or not first
		 */
		/**
		 * download new data first, after check already download or not
		 */
		sumDataMonths();

		/**
		 * Save days index into single ton way to check show backward or forward
		 * button or not
		 */
		if (!DashBoardFragment.IS_RESUME_FROM_BACKGROUND) {
			Calendar mCalendarToday = Calendar.getInstance();
			int today = mCalendarToday.get(Calendar.DAY_OF_MONTH);
			if (MONTHS == 0) {
				DashBoardFragment.singleTon.setOffsetMonths(today);
				INDEX_MONTHS = DashBoardFragment.singleTon.getOffsetMonths();
			} else {
				if (!left_right) {
					DashBoardFragment.singleTon.setOffsetMonths(INDEX_MONTHS
							+ MONTHS * 90);
				} else {
					DashBoardFragment.singleTon.setOffsetMonths(INDEX_MONTHS
							- MONTHS * 90);
				}
				INDEX_MONTHS = DashBoardFragment.singleTon.getOffsetMonths();
			}

			DashBoardFragment.singleTon.setIndexMonths(INDEX_MONTHS);
		}
	}

	private void getNextWeek(int WEEK) {
		if (WEEK == 0) {
			mTvDate.setText(Utils.getCurrentWeek(getActivity(),
					DashBoardFragment.mCalendarWeek));
		} else {
			if (DashBoardFragment.IS_RESUME_FROM_BACKGROUND) {
				mTvDate.setText(Utils.getNextWeek(getActivity(),
						DashBoardFragment.mCalendarWeek, 1, 6));
			} else {
				mTvDate.setText(Utils.getNextWeek(getActivity(),
						DashBoardFragment.mCalendarWeek, 1, 6));
			}
		}

		DashBoardFragment.singleTon
				.setCurrentDate(mTvDate.getText().toString());

		sumDataWeek();

		/**
		 * Save days index into single ton way to check show backward or forward
		 * button or not
		 */
		if (!DashBoardFragment.IS_RESUME_FROM_BACKGROUND) {
			if (WEEK == 0) {
				DashBoardFragment.singleTon.setOffsetWeek(7);
				INDEX_WEEK = 7;
			} else {
				DashBoardFragment.singleTon.setOffsetWeek(INDEX_WEEK - 7);
				INDEX_WEEK = DashBoardFragment.singleTon.getOffsetWeek();
			}

			DashBoardFragment.singleTon.setIndexWeek(INDEX_WEEK);
		}
	}

	private void getYear(int YEAR) {
		if (YEAR == 0) {
			// Set text for day
			mTvDate.setText(Utils.getCurrentYear().toUpperCase());
		} else {
			String DATE = Utils.getLastYear(YEAR).toUpperCase() + "";
			// Set text for day
			mTvDate.setText(DATE);
		}

		DashBoardFragment.singleTon
				.setCurrentDate(mTvDate.getText().toString());
		setCurrentDateInYear(mTvDate.getText().toString());
		/**
		 * Check already saved data or not first
		 */
		/**
		 * Download new data first, after check already download or not
		 */
		sumDataYear();
		/**
		 * Save days index into single ton way to check show backward or forward
		 * button or not
		 */
		if (YEAR == 0) {
			DashBoardFragment.singleTon.setOffsetYear(365);
		} else {
			DashBoardFragment.singleTon.setOffsetYear(YEAR * 365);
		}

		INDEX_YEAR = DashBoardFragment.singleTon.getOffsetYear();
		DashBoardFragment.singleTon.setIndexYear(INDEX_YEAR);
	}

	private void resetDateIndex() {
		// Reset chosen days index from Single ton
		singleTon.setOffsetMonth(0);
		singleTon.setOffsetMonths(0);
		singleTon.setOffsetWeek(0);
		singleTon.setOffsetYear(0);

		// Day
		DAY = 0;
		TOMORROW = 0;
		YESTERDAY = 0;

		// 1 Month
		MONTH = 0;
		NEXT_MONTH = 0;
		PREVIOUS_MONTH = 0;

		// 3 Months
		MONTHS = 0;
		NEXT_MONTHS = 0;
		PREVIOUS_MONTHS = 0;

		// Week
		WEEK = 0;
		NEXT_WEEK = 0;
		PREVIOUS_WEEK = 0;

		// Year
		YEAR = 0;
		NEXT_YEAR = 0;
		PREVIOUS_YEAR = 0;
	}

	private void resetGoalValue() {
		singleTon.setCaloriesGoal(singleTon.getCaloriesGoalDefault());
		singleTon.setDistanceGoal(singleTon.getDistanceGoalDefault());
		singleTon.setSleepGoal(singleTon.getSleepGoalDefault());
		singleTon.setStepsGoal(singleTon.getStepsGoalDefault());
	}

	private static void saveActivitiesCurrentValueIntoSingleTon(
			String calories, String distance, String steps) {
		singleTon.setCaloriesCurrent(calories.replace(",", ""));
		singleTon.setDistanceCurrent(distance.replace(",", ""));
		singleTon.setStepsCurrent(steps.replace(",", ""));
	}

	private static void saveActivitiesGoalValueIntoSingleTon(String calories,
			String distance, String steps) {
		if (!calories.equals("") & !distance.equals("") & !steps.equals("")) {
			singleTon.setCaloriesGoal(calories.replace(",", ""));
			singleTon.setDistanceGoal(distance.replace(",", ""));
			singleTon.setStepsGoal(steps.replace(",", ""));
		}
	}

	private void saveAllDataIntoSingleTon() {
		singleTon.setCurrentDate(mTvDate.getText().toString());
		singleTon
				.setCurrentTimePicker(DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD);

		// Current value of activities
		saveActivitiesCurrentValueIntoSingleTon(mTvCaloriesTitle.getText()
				.toString(), mTvDistanceTitle.getText().toString(),
				mTvStepsTitle.getText().toString());
		// Goal value of activities
		saveActivitiesGoalValueIntoSingleTon(mTvCaloriesContent.getText()
				.toString(), mTvDistanceContent.getText().toString(),
				mTvStepsContent.getText().toString());

		// Current value of sleep
		saveSleepCurrentValueIntoSingleTon(mTvSleepHoursTitle.getText()
				.toString(), mTvSleepMinutesTitle.getText().toString());
		// Goal value of sleep
		saveSleepGoalValueIntoSingleTon(mTvSleepHoursContent.getText()
				.toString().replace("h", ""));

		// Day option picker
		singleTon.setDay(DAY);
		singleTon.setYesterday(YESTERDAY);
		singleTon.setTomorrow(TOMORROW);

		// Month option picker
		singleTon.setIndexMonth(INDEX_MONTH);
		singleTon.setMonth(MONTH);
		singleTon.setPreviousMonth(PREVIOUS_MONTH);
		singleTon.setNextMonth(NEXT_MONTH);

		// Months option picker
		singleTon.setIndexMonths(INDEX_MONTHS);
		singleTon.setMonths(MONTHS);
		singleTon.setPreviousMonths(PREVIOUS_MONTHS);
		singleTon.setNextMonths(NEXT_MONTHS);

		// Week option picker
		singleTon.setIndexWeek(INDEX_WEEK);
		singleTon.setWeek(WEEK);
		singleTon.setPreviousWeek(PREVIOUS_WEEK);
		singleTon.setNextWeek(NEXT_WEEK);

		// Year option picker
		singleTon.setIndexYear(INDEX_YEAR);
		singleTon.setYear(YEAR);
		singleTon.setPreviousYear(PREVIOUS_YEAR);
		singleTon.setNextYear(NEXT_YEAR);
	}

	private static void saveChartDataMonth() {
		singleTon.setMonthCalories(FitnessData.MONTH_CALORIES);
		singleTon.setMonthDistance(FitnessData.MONTH_DISTANCE);
		singleTon.setMonthSteps(FitnessData.MONTH_STEPS);
	}

	private void saveChartDataMonths() {
		singleTon.setMonthsCalories(FitnessData.MONTHS_CALORIES);
		singleTon.setMonthsDistance(FitnessData.MONTHS_DISTANCE);
		singleTon.setMonthsSteps(FitnessData.MONTHS_STEPS);
	}

	private void saveChartDataWeek() {
		singleTon.setWeekCalories(FitnessData.WEEK_CALORIES);
		singleTon.setWeekDistance(FitnessData.WEEK_DISTANCE);
		singleTon.setWeekSteps(FitnessData.WEEK_STEPS);
	}

	private void saveChartDataYear() {
		singleTon.setYearCalories(FitnessData.YEAR_CALORIES);
		singleTon.setYearDistance(FitnessData.YEAR_DISTANCE);
		singleTon.setYearSteps(FitnessData.YEAR_STEPS);
	}

	private static void saveSleepCurrentValueIntoSingleTon(
			String sleep_hour_title, String sleep_minute_title) {
		singleTon.setTotalSleepHours(sleep_hour_title);
		singleTon.setTotalSleepMinutes(sleep_minute_title);
	}

	private static void saveSleepGoalValueIntoSingleTon(String sleep) {
		if (!sleep.equals("")) {
			singleTon.setSleepGoal(sleep);
		}
	}

	private void setBackgroundDialog(int key) {
		int bg = getResources().getColor(R.color.green_text);
		switch (key) {
		case Key.KEY_1_MONTH:
			mLl1Month.setBackgroundColor(bg);
			mLl3Months.setBackgroundColor(Color.BLACK);
			mLlDaily.setBackgroundColor(Color.BLACK);
			mLlWeekly.setBackgroundColor(Color.BLACK);
			mLlYearly.setBackgroundColor(Color.BLACK);
			break;
		case Key.KEY_3_MONTHS:
			mLl1Month.setBackgroundColor(Color.BLACK);
			mLl3Months.setBackgroundColor(bg);
			mLlDaily.setBackgroundColor(Color.BLACK);
			mLlWeekly.setBackgroundColor(Color.BLACK);
			mLlYearly.setBackgroundColor(Color.BLACK);
			break;
		case Key.KEY_DAILY:
			mLl1Month.setBackgroundColor(Color.BLACK);
			mLl3Months.setBackgroundColor(Color.BLACK);
			mLlDaily.setBackgroundColor(bg);
			mLlWeekly.setBackgroundColor(Color.BLACK);
			mLlYearly.setBackgroundColor(Color.BLACK);
			break;
		case Key.KEY_WEEKLY:
			mLl1Month.setBackgroundColor(Color.BLACK);
			mLl3Months.setBackgroundColor(Color.BLACK);
			mLlDaily.setBackgroundColor(Color.BLACK);
			mLlWeekly.setBackgroundColor(bg);
			mLlYearly.setBackgroundColor(Color.BLACK);
			break;
		case Key.KEY_YEARLY:
			mLl1Month.setBackgroundColor(Color.BLACK);
			mLl3Months.setBackgroundColor(Color.BLACK);
			mLlDaily.setBackgroundColor(Color.BLACK);
			mLlWeekly.setBackgroundColor(Color.BLACK);
			mLlYearly.setBackgroundColor(bg);
			break;
		default:
			break;
		}
	}

	private void setCurrentDateInMonth(Calendar mCalendarDate) {
		DashBoardFragment.singleTon
		// .setCurrentMonth(Month.month_text[mCalendarDate
		// .get(Calendar.MONTH)]);
				.setCurrentMonth(Utils.getCorrectMonth(getActivity(),
						mCalendarDate, -1));
		DashBoardFragment.singleTon.setCurrentYear(mCalendarDate
				.get(Calendar.YEAR) + "");
	}

	private void setCurrentDateInMonths(Calendar mCalendarFirstMonth,
			Calendar mCalendarLastMonth) {
		DashBoardFragment.singleTon
		// .setPrevious3Months(Month.month_text[mCalendarFirstMonth
		// .get(Calendar.MONTH)]);
				.setPrevious3Months(Utils.getCorrectMonth(getActivity(),
						mCalendarFirstMonth, -1));
		DashBoardFragment.singleTon
		// .setNext3Months(Month.month_text[mCalendarLastMonth
		// .get(Calendar.MONTH)]);
				.setNext3Months(Utils.getCorrectMonth(getActivity(),
						mCalendarLastMonth, -1));
		DashBoardFragment.singleTon.setCurrentYear(mCalendarFirstMonth
				.get(Calendar.YEAR) + "");
	}

	private void setCurrentDateInYear(String DATE) {
		DashBoardFragment.singleTon.setCurrentYear(DATE.trim());
	}

	public static void setFitnessData(String calories, String distance,
			String steps) {
		mTvCaloriesTitle.setText(Utils.showIntergerFormat(calories + ""));
		mTvDistanceTitle.setText(Utils.showIntergerFormat(Utils
				.getFloatFormat(Float.valueOf(distance)) + ""));
		mTvStepsTitle.setText(Utils.showIntergerFormat(steps + ""));
	}

	public static void setFitnessData(Bundle savedInstanceState) {
		// Set Activities information
		if (mTvCaloriesTitle != null) {
			mTvCaloriesTitle.setText(Utils
					.showIntergerFormat(savedInstanceState
							.getString(Key.CALORIES_CURRENT)));
		}
		if (mTvDistanceTitle != null) {
			mTvDistanceTitle.setText(Utils
					.showIntergerFormat(savedInstanceState
							.getString(Key.DISTANCE_CURRENT)));
		}
		if (mTvStepsTitle != null) {
			mTvStepsTitle.setText(Utils.showIntergerFormat(savedInstanceState
					.getString(Key.STEPS_CURRENT)));
		}

		// Set current time picker
		DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = savedInstanceState
				.getInt(Key.CURRENT_TIME_PICKER);

		if (mTvDate != null) {
			// Set date
			mTvDate.setText(savedInstanceState.getString(Key.DATE));
			DashBoardFragment.singleTon.setCurrentDate(mTvDate.getText()
					.toString());
		}
	}

	private void setRefreshDailyDataTimer() {
		final Handler handler = new Handler();
		handler.post(new Runnable() {

			@Override
			public void run() {
				if (IS_IN_DASHBOARD) {
					/**
					 * Check if Today, always refresh after 5 minutes to get new
					 * data
					 */
					if (ActivityFitness.mFm
							.findFragmentById(R.id.fr_content_activity_fitness)
							.getClass().getSimpleName()
							.equals(DashBoardFragment.class.getSimpleName())
							& singleTon.getCurrentDate().equals(
									getString(R.string.today)
											+ " "
											+ Utils.getCurrentDate(
													getActivity())
													.toUpperCase())
							& singleTon.getCurrentTimePicker() == Key.KEY_DAILY) {
						// Refresh data of today
						getFitnessDataInDays(0);
					}

					/**
					 * Refresh again after 5 minutes
					 */
					handler.postDelayed(this, 1000 * 60 * 5);
				}
			}
		});
	}

	private void setTimerToGetDataAfterSignedIn(final Bundle savedInstanceState) {
		final Handler handler = new Handler();
		handler.post(new Runnable() {

			@Override
			public void run() {
				/**
				 * Do nothing if current fragment is not Dash Board fragment
				 */
				if (AboutFragment.IS_IN_ABOUT_FRAGMENT
						| GoalSettingsFragment.IS_IN_GOAL_PAGE) {
					return;
				}

				/**
				 * Already signed in account, begin get data
				 */
				if (IS_SIGNED_IN & IS_IN_DASHBOARD & getActivity() != null) {
					// Remove timer
					handler.removeCallbacks(this);

					/**
					 * Step 1 : If had not existed data yet Download data for
					 * 365 days to store first, after that can extract followed
					 * the date was matched in Date Time
					 */
					if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
						if (FitnessHistoryData.fitness_history_data[0] == null) {
							/**
							 * Always save this array, not clear. To extract
							 * data
							 */
							getFitnessDataAllDaysInYear(365);
						}
					}

					/**
					 * From here, go to Goal page when open application in the
					 * second time
					 */
					IS_GO_FROM_DASHBOARD_TO_GOAL_PAGE = true;

					// Set default item in Action Sheet
					DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD = Key.KEY_DAILY;

					/**
					 * Set timer will be refresh after 5 minutes in TODAY
					 */
					setRefreshDailyDataTimer();

					// Update data on UI
					getAllData(savedInstanceState);
				} else {
					/**
					 * Refresh again after 7 seconds
					 */
					handler.postDelayed(this, 7000);
				}
			}
		});
	}

	private void showDataOnPage() {
		/**
		 * Check if go from Dash board Detail page
		 */
		if (DashBoardDetailFragment.IS_GO_TO_DASHBOARD_FROM_DASHBOARD_DETAIL) {
			DashBoardDetailFragment.IS_GO_TO_DASHBOARD_FROM_DASHBOARD_DETAIL = false;

			// Get data after back from Dash board detail page
			getDataValueFromDashBoardDetailPage();
		} else {
			/**
			 * Load Default Goal value and show on page
			 */
			// Set Goal data
			showGoalValueOnPage();

			switch (DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD) {
			case Key.KEY_DAILY:
				if (DAY == 0) {
					mIbtnRight.setVisibility(View.INVISIBLE);

					// Set text for day
					mTvDate.setText(singleTon.getCurrentDate());
					DashBoardFragment.singleTon.setCurrentDate(mTvDate
							.getText().toString());
				}
				break;
			default:
				break;
			}

			/**
			 * If go from Goal page, update again value for Daily
			 */
			if (GoalSettingsFragment.IS_GO_TO_DASHBOARD_PAGE_FROM_GOAL_PAGE
					| AboutFragment.IS_GO_TO_DASHBOARD_PAGE_FROM_ABOUT_PAGE) {
				AboutFragment.IS_GO_TO_DASHBOARD_PAGE_FROM_ABOUT_PAGE = false;
				GoalSettingsFragment.IS_GO_TO_DASHBOARD_PAGE_FROM_GOAL_PAGE = false;

				resetDateIndex();
				resetGoalValue();

				mIbtnRight.setVisibility(View.INVISIBLE);
				// Set text for day
				mTvDate.setText(getString(R.string.today) + " "
						+ Utils.getCurrentDate(getActivity()).toUpperCase()
						+ "");
				singleTon.setCurrentDate(mTvDate.getText().toString());
			}
		}
	}

	private void showGoalValueOnPage() {
		/**
		 * Load Default Goal value from Shared preferences
		 */
		if (IS_FIRST_TIME_LOAD_DATA_FROM_SHARED_PREFERENCES) {
			IS_FIRST_TIME_LOAD_DATA_FROM_SHARED_PREFERENCES = false;

			mSp = getActivity().getSharedPreferences(
					Constants.PREFERENCES_GOALS, 0);

			mTvCaloriesContent.setText(Utils.showIntergerFormat(mSp.getString(
					Key.CALORIES_GOAL, getString(R.integer.calories_default))));
			mTvDistanceContent.setText(Utils.showIntergerFormat(mSp.getString(
					Key.DISTANCE_GOAL, getString(R.integer.distance_default))));
			mTvSleepHoursContent.setText(Utils.showIntergerFormat(mSp
					.getString(Key.SLEEP_HOURS_CONTENT,
							getString(R.integer.sleep_default)))
					+ "h");
			mTvStepsContent.setText(Utils.showIntergerFormat(mSp.getString(
					Key.STEPS_GOAL, getString(R.integer.steps_default))));

			/**
			 * Set into Default Goal Value also
			 */
			singleTon.setCaloriesGoalDefault(mTvCaloriesContent.getText()
					.toString().replace(",", "").trim());
			singleTon.setDistanceGoalDefault(mTvDistanceContent.getText()
					.toString().replace(",", "").trim());
			singleTon.setSleepGoalDefault(mTvSleepHoursContent.getText()
					.toString().replace("h", "").trim());
			singleTon.setStepsGoalDefault(mTvStepsContent.getText().toString()
					.replace(",", "").trim());

			saveActivitiesGoalValueIntoSingleTon(mTvCaloriesContent.getText()
					.toString().replace(",", "").trim(), mTvDistanceContent.getText()
					.toString().replace(",", "").trim(), mTvStepsContent.getText()
					.toString().replace(",", "").trim());
			saveSleepGoalValueIntoSingleTon(mTvSleepHoursContent.getText()
					.toString().replace("h", "").trim());

			return;
		}

		/**
		 * Show Goal value
		 */
		long calories = Long.valueOf(singleTon.getCaloriesGoalDefault());
		float distance = Float.valueOf(singleTon.getDistanceGoalDefault());
		int sleep = Integer.valueOf(singleTon.getSleepGoalDefault());
		long steps = Long.valueOf(singleTon.getStepsGoalDefault());

		mTvCaloriesContent.setText(Utils.showIntergerFormat(calories + ""));
		mTvDistanceContent.setText(Utils.showIntergerFormat(distance + ""));
		mTvSleepHoursContent.setText(sleep + "h");
		mTvStepsContent.setText(Utils.showIntergerFormat(steps + ""));
	}

	public static void showTimeLine(Context context) {
		switch (DashBoard.CHOOSE_DATE_IN_ACTION_SHEET_DASH_BOARD) {
		case Key.KEY_1_MONTH:
			showTimeLineForMonth(context);
			break;
		case Key.KEY_3_MONTHS:
			showTimeLineForMonths(context);
			break;
		case Key.KEY_DAILY:
			showTimeLineForDaily(context);
			break;
		case Key.KEY_WEEKLY:
			showTimeLineForWeekly();
			break;
		case Key.KEY_YEARLY:
			showTimeLineForYearly();
			break;
		default:
			break;
		}
	}

	public static void showTimeLineForDaily(Context context) {
		switch (mViewPager.getCurrentItem()) {
		case Position.CALORIES:
			// Calories page
			if (DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths != null) {
				DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.VISIBLE);
				DashBoardCaloriesSimpleFragment.mLlTimeWeekly
						.setVisibility(View.GONE);
				DashBoardCaloriesSimpleFragment.mLlTimeYearly
						.setVisibility(View.GONE);

				// Set value for time line
				DashBoardCaloriesSimpleFragment.mTvFirstDay.setText(context
						.getString(R.string.AM12));
				DashBoardCaloriesSimpleFragment.mTvMiddleDay.setText(context
						.getString(R.string.PM));
				DashBoardCaloriesSimpleFragment.mTvLastDay.setText(context
						.getString(R.string.AM12));
			}
			break;
		case Position.DISTANCE:
			// Distance page
			if (DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths != null) {
				DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.VISIBLE);
				DashBoardDistanceSimpleFragment.mLlTimeWeekly
						.setVisibility(View.GONE);
				DashBoardDistanceSimpleFragment.mLlTimeYearly
						.setVisibility(View.GONE);

				// Set value for time line
				DashBoardDistanceSimpleFragment.mTvFirstDay.setText(context
						.getString(R.string.AM12));
				DashBoardDistanceSimpleFragment.mTvMiddleDay.setText(context
						.getString(R.string.PM));
				DashBoardDistanceSimpleFragment.mTvLastDay.setText(context
						.getString(R.string.AM12));
			}
			break;
		case Position.SLEEP:
			if (DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths != null) {
				// Sleep page
				DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.VISIBLE);

				DashBoardSleepSimpleFragment.mTvFirstDay.setText(context
						.getString(R.string.AM12));
				DashBoardSleepSimpleFragment.mTvMiddleDay.setText(context
						.getString(R.string.PM));
				DashBoardSleepSimpleFragment.mTvLastDay.setText(context
						.getString(R.string.AM12));
			}
			break;
		case Position.STEPS:
			// Steps page
			if (DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths != null) {
				DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.VISIBLE);
				DashBoardStepsSimpleFragment.mLlTimeWeekly
						.setVisibility(View.GONE);
				DashBoardStepsSimpleFragment.mLlTimeYearly
						.setVisibility(View.GONE);

				// Set value for time line
				DashBoardStepsSimpleFragment.mTvFirstDay.setText(context
						.getString(R.string.AM12));
				DashBoardStepsSimpleFragment.mTvMiddleDay.setText(context
						.getString(R.string.PM));
				DashBoardStepsSimpleFragment.mTvLastDay.setText(context
						.getString(R.string.AM12));
			}
			break;
		}
	}

	public static void showTimeLineForMonth(Context context) {
		Calendar mCalendar = Calendar.getInstance(); // Current time
		mCalendar.set(Calendar.MONTH, singleTon.getCurrentMonthIndex(context));
		int max_days_of_month = mCalendar
				.getActualMaximum(Calendar.DAY_OF_MONTH);

		switch (mViewPager.getCurrentItem()) {
		case Position.CALORIES:
			// Calories page
			if (DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths != null) {
				DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.VISIBLE);
				DashBoardCaloriesSimpleFragment.mLlTimeWeekly
						.setVisibility(View.GONE);
				DashBoardCaloriesSimpleFragment.mLlTimeYearly
						.setVisibility(View.GONE);

				// Set value for time line
				DashBoardCaloriesSimpleFragment.mTvFirstDay.setText("1 "
						+ singleTon.getCurrentMonth());
				DashBoardCaloriesSimpleFragment.mTvMiddleDay.setText("15 "
						+ singleTon.getCurrentMonth());
				DashBoardCaloriesSimpleFragment.mTvLastDay
						.setText(max_days_of_month + " "
								+ singleTon.getCurrentMonth());
			}
			break;
		case Position.DISTANCE:
			// Distance page
			if (DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths != null) {
				DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.VISIBLE);
				DashBoardDistanceSimpleFragment.mLlTimeWeekly
						.setVisibility(View.GONE);
				DashBoardDistanceSimpleFragment.mLlTimeYearly
						.setVisibility(View.GONE);

				// Set value for time line
				DashBoardDistanceSimpleFragment.mTvFirstDay.setText("1 "
						+ singleTon.getCurrentMonth());
				DashBoardDistanceSimpleFragment.mTvMiddleDay.setText("15 "
						+ singleTon.getCurrentMonth());
				DashBoardDistanceSimpleFragment.mTvLastDay
						.setText(max_days_of_month + " "
								+ singleTon.getCurrentMonth());
			}
			break;
		case Position.SLEEP:
			// Sleep page
			if (DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths != null) {
				DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.GONE);
			}
			break;
		case Position.STEPS:
			// Steps page
			if (DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths != null) {
				DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths
						.setVisibility(View.VISIBLE);
				DashBoardStepsSimpleFragment.mLlTimeWeekly
						.setVisibility(View.GONE);
				DashBoardStepsSimpleFragment.mLlTimeYearly
						.setVisibility(View.GONE);

				// Set value for time line
				DashBoardStepsSimpleFragment.mTvFirstDay.setText("1 "
						+ singleTon.getCurrentMonth());
				DashBoardStepsSimpleFragment.mTvMiddleDay.setText("15 "
						+ singleTon.getCurrentMonth());
				DashBoardStepsSimpleFragment.mTvLastDay
						.setText(max_days_of_month + " "
								+ singleTon.getCurrentMonth());
			}
			break;
		}
	}

	public static void showTimeLineForMonths(Context context) {
		// Calories page
		if (DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths != null) {
			DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.VISIBLE);
			DashBoardCaloriesSimpleFragment.mLlTimeWeekly
					.setVisibility(View.GONE);
			DashBoardCaloriesSimpleFragment.mLlTimeYearly
					.setVisibility(View.GONE);

			// Set value for time line
			DashBoardCaloriesSimpleFragment.mTvFirstDay.setText(singleTon
					.getPrevious3Months());
			DashBoardCaloriesSimpleFragment.mTvMiddleDay.setText(Utils
					.getCorrectMonth(context, null,
							singleTon.getCurrentMiddleMonthIndex()));
			DashBoardCaloriesSimpleFragment.mTvLastDay.setText(singleTon
					.getNext3Months());
		}

		// Distance page
		if (DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths != null) {
			DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.VISIBLE);
			DashBoardDistanceSimpleFragment.mLlTimeWeekly
					.setVisibility(View.GONE);
			DashBoardDistanceSimpleFragment.mLlTimeYearly
					.setVisibility(View.GONE);

			// Set value for time line
			DashBoardDistanceSimpleFragment.mTvFirstDay.setText(singleTon
					.getPrevious3Months());
			DashBoardDistanceSimpleFragment.mTvMiddleDay.setText(Utils
					.getCorrectMonth(context, null,
							singleTon.getCurrentMiddleMonthIndex()));
			DashBoardDistanceSimpleFragment.mTvLastDay.setText(singleTon
					.getNext3Months());
		}

		// Sleep page
		if (DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths != null) {
			DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
		}

		// Set value for time line

		// Steps page
		if (DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths != null) {
			DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.VISIBLE);
			DashBoardStepsSimpleFragment.mLlTimeWeekly.setVisibility(View.GONE);
			DashBoardStepsSimpleFragment.mLlTimeYearly.setVisibility(View.GONE);

			DashBoardStepsSimpleFragment.mTvFirstDay.setText(singleTon
					.getPrevious3Months());
			DashBoardStepsSimpleFragment.mTvMiddleDay.setText(Utils
					.getCorrectMonth(context, null,
							singleTon.getCurrentMiddleMonthIndex()));
			DashBoardStepsSimpleFragment.mTvLastDay.setText(singleTon
					.getNext3Months());
		}
	}

	public static void showTimeLineForWeekly() {
		// Calories page
		if (DashBoardCaloriesSimpleFragment.mLlTimeWeekly != null) {
			DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
			DashBoardCaloriesSimpleFragment.mLlTimeWeekly
					.setVisibility(View.VISIBLE);
			DashBoardCaloriesSimpleFragment.mLlTimeYearly
					.setVisibility(View.GONE);
		}

		// Distance page
		if (DashBoardDistanceSimpleFragment.mLlTimeWeekly != null) {
			DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
			DashBoardDistanceSimpleFragment.mLlTimeWeekly
					.setVisibility(View.VISIBLE);
			DashBoardDistanceSimpleFragment.mLlTimeYearly
					.setVisibility(View.GONE);
		}

		// Sleep page
		if (DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths != null) {
			DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
		}

		// Steps page
		if (DashBoardStepsSimpleFragment.mLlTimeWeekly != null) {
			DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
			DashBoardStepsSimpleFragment.mLlTimeWeekly
					.setVisibility(View.VISIBLE);
			DashBoardStepsSimpleFragment.mLlTimeYearly.setVisibility(View.GONE);
		}
	}

	public static void showTimeLineForYearly() {
		// Calories page
		if (DashBoardCaloriesSimpleFragment.mLlTimeYearly != null) {
			DashBoardCaloriesSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
			DashBoardCaloriesSimpleFragment.mLlTimeWeekly
					.setVisibility(View.GONE);
			DashBoardCaloriesSimpleFragment.mLlTimeYearly
					.setVisibility(View.VISIBLE);
		}

		// Distance page
		if (DashBoardDistanceSimpleFragment.mLlTimeYearly != null) {
			DashBoardDistanceSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
			DashBoardDistanceSimpleFragment.mLlTimeWeekly
					.setVisibility(View.GONE);
			DashBoardDistanceSimpleFragment.mLlTimeYearly
					.setVisibility(View.VISIBLE);
		}

		// Sleep page
		if (DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths != null) {
			DashBoardSleepSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
		}

		// Steps page
		if (DashBoardStepsSimpleFragment.mLlTimeYearly != null) {
			DashBoardStepsSimpleFragment.mLlTimeDayMonthMonths
					.setVisibility(View.GONE);
			DashBoardStepsSimpleFragment.mLlTimeWeekly.setVisibility(View.GONE);
			DashBoardStepsSimpleFragment.mLlTimeYearly
					.setVisibility(View.VISIBLE);
		}
	}

	private void sumDataMonth(int picker_month) {
		// Clear old data before add new data
		clearOldData();

		/**
		 * Get current month info
		 */
		Calendar mCalendarCurrent = Calendar.getInstance();

		int today = mCalendarCurrent.get(Calendar.DAY_OF_YEAR);
		int today_check = mCalendarCurrent.get(Calendar.DAY_OF_MONTH);
		int current_month = mCalendarCurrent.get(Calendar.MONTH);
		int current_year = mCalendarCurrent.get(Calendar.YEAR);

		/**
		 * Find out the month and year of selection
		 */
		int picker_year = Integer.valueOf(DashBoardFragment.singleTon
				.getCurrentYear());
		// Store index of current month view
		DashBoardFragment.singleTon.setCurrentMonthIndex(picker_month);

		/**
		 * Initialize picker month
		 */
		mCalendarCurrent.set(Calendar.DATE, 1); // set to first day
		mCalendarCurrent.set(Calendar.MONTH, picker_month);
		mCalendarCurrent.set(Calendar.YEAR, picker_year);
		mCalendarCurrent.set(Calendar.HOUR_OF_DAY, 0);
		mCalendarCurrent.set(Calendar.MINUTE, 0);
		mCalendarCurrent.set(Calendar.SECOND, 0);

		/**
		 * Get picker month info
		 */
		// First day of month
		int first_day_of_picker_month_in_year = mCalendarCurrent
				.get(Calendar.DAY_OF_YEAR);
		int first_day_of_picker_month = mCalendarCurrent
				.get(Calendar.DAY_OF_MONTH);
		// Last day of month
		int max_day_of_picker_month = mCalendarCurrent
				.getActualMaximum(Calendar.DAY_OF_MONTH);

		/**
		 * Initialize array follow max days of month
		 */
		SleepData.MONTH_DEEP_SLEEP = new int[max_day_of_picker_month];
		SleepData.MONTH_LIGHT_SLEEP = new int[max_day_of_picker_month];
		SleepData.MONTH_TOTAL_SLEEP = new int[max_day_of_picker_month];

		int SUM_CALORIES = 0;
		float SUM_DISTANCE = 0;
		int SUM_STEPS = 0;

		int total_deep_sleep_minutes = 0;
		int total_light_sleep_minutes = 0;
		int total_sleep_minutes = 0;

		if ((current_month == picker_month) && (current_year == picker_year)) {
			/**
			 * If be current month, need to calculate from first day of month to
			 * current day
			 */
			// count number of days till today
			int days = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
			// Sum calories, distance, steps
			int CALORIES = 0;
			float DISTANCE = 0;
			int STEPS = 0;

			int light_sleep_minutes = 0;
			int deep_sleep_minutes = 0;
			int sleep_minutes = 0;

			/**
			 * If it is today, need get data of today to add into Weekly view
			 */
			int offset_from_today = 0;
			if (today_check == 1) {
				FitnessData.MONTH_CALORIES[0] += Integer
						.valueOf(DashBoardFragment.singleTon.getCaloriesToday());
				FitnessData.MONTH_DISTANCE[0] += Float
						.valueOf(DashBoardFragment.singleTon.getDistanceToday());
				FitnessData.MONTH_STEPS[0] += Integer
						.valueOf(DashBoardFragment.singleTon.getStepsToday());

				// Sum all item data value before show on Page
				SUM_CALORIES += FitnessData.MONTH_CALORIES[0];
				SUM_DISTANCE += FitnessData.MONTH_DISTANCE[0];
				SUM_STEPS += FitnessData.MONTH_STEPS[0];
			} else {
				offset_from_today = days - 1;
				for (int i = 0; i < days; i++) {
					if (offset_from_today > FitnessHistoryData.fitness_history_data.length) {
						break;
					}

					if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
						if (FitnessHistoryData.fitness_history_data[offset_from_today]
								.getLocalDate() != null) {

							/**
							 * Some days don't have data, so need check data not
							 * exceed first day of month & last day of month
							 */
							Calendar mCalendar = Calendar.getInstance();
							mCalendar
									.setTime(FitnessHistoryData.fitness_history_data[offset_from_today]
											.getLocalDate());
							if (mCalendar.get(Calendar.DAY_OF_MONTH) >= first_day_of_picker_month
									& mCalendar.get(Calendar.DAY_OF_MONTH) <= max_day_of_picker_month
									& mCalendar.get(Calendar.MONTH) == picker_month) {
								/**
								 * Activities value
								 */
								CALORIES = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getCaloriesCurrent());
								DISTANCE = Float
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getDistanceCurrent());
								STEPS = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getStepsCurrent());

								// Add into array to draw on chart
								FitnessData.MONTH_CALORIES[i] = CALORIES;
								FitnessData.MONTH_DISTANCE[i] = DISTANCE;
								FitnessData.MONTH_STEPS[i] = STEPS;

								// Sum all item data value before show on Page
								SUM_CALORIES += CALORIES;
								SUM_DISTANCE += DISTANCE;
								SUM_STEPS += STEPS;

								offset_from_today--;

								/**
								 * If it is today, need get data of today to add
								 * into Weekly view
								 */
								if (offset_from_today == 0) {
									FitnessData.MONTH_CALORIES[i + 1] += Integer
											.valueOf(DashBoardFragment.singleTon
													.getCaloriesToday());
									FitnessData.MONTH_DISTANCE[i + 1] += Float
											.valueOf(DashBoardFragment.singleTon
													.getDistanceToday());
									FitnessData.MONTH_STEPS[i + 1] += Integer
											.valueOf(DashBoardFragment.singleTon
													.getStepsToday());

									// Sum all item data value before show on
									// Page
									SUM_CALORIES += FitnessData.MONTH_CALORIES[i + 1];
									SUM_DISTANCE += FitnessData.MONTH_DISTANCE[i + 1];
									SUM_STEPS += FitnessData.MONTH_STEPS[i + 1];

									break;
								}
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					} else {
						offset_from_today--;
					}
				}
			}

			/**
			 * If it is today, need get data of today to add into Weekly view
			 */
			if (today_check == 1) {
				int deep_sleep = (Integer.valueOf(DashBoardFragment.singleTon
						.getSleepTodayDeepHours()) * 60 + Integer
						.valueOf(DashBoardFragment.singleTon
								.getSleepTodayDeepMinutes()));
				int light_sleep = (Integer.valueOf(DashBoardFragment.singleTon
						.getSleepTodayLightHours()) * 60 + Integer
						.valueOf(DashBoardFragment.singleTon
								.getSleepTodayLightMinutes()));

				SleepData.MONTH_DEEP_SLEEP[0] = deep_sleep;
				SleepData.MONTH_LIGHT_SLEEP[0] = light_sleep;
				SleepData.MONTH_TOTAL_SLEEP[0] = (deep_sleep + light_sleep);

				total_deep_sleep_minutes += deep_sleep;
				total_light_sleep_minutes += light_sleep;
				total_sleep_minutes += (deep_sleep + light_sleep);
			} else {
				/**
				 * Sleep values
				 */
				offset_from_today = days - 1;
				for (int i = 0; i < days; i++) {
					if (offset_from_today > SleepData.sleep_history_data.length) {
						break;
					}

					if (SleepData.sleep_history_data[offset_from_today] != null) {
						/**
						 * Need check here, current day can not pass first day
						 * of month
						 */
						/**
						 * Sleep values
						 */
						if (SleepData.sleep_history_data[offset_from_today]
								.getRecordDate() != null) {
							Calendar mCalendarDayHasData = Calendar
									.getInstance();
							mCalendarDayHasData
									.setTime(SleepData.sleep_history_data[offset_from_today]
											.getRecordDate());
							int current_day = mCalendarDayHasData
									.get(Calendar.DAY_OF_MONTH);
							int current_month_has_data = mCalendarDayHasData
									.get(Calendar.MONTH);

							// Run from today to first day of month
							if (current_day >= first_day_of_picker_month
									& current_month_has_data == current_month) {
								light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getLightSleep();
								deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getDeepSleep();
								sleep_minutes = light_sleep_minutes
										+ deep_sleep_minutes;

								SleepData.MONTH_DEEP_SLEEP[i] = deep_sleep_minutes;
								SleepData.MONTH_LIGHT_SLEEP[i] = light_sleep_minutes;
								SleepData.MONTH_TOTAL_SLEEP[i] = sleep_minutes;

								// Sum all item data value before show on Page
								total_light_sleep_minutes += light_sleep_minutes;
								total_deep_sleep_minutes += deep_sleep_minutes;
								total_sleep_minutes += sleep_minutes;
							}

							offset_from_today--;

							/**
							 * Need plus total sleep, deep sleep, light sleep of
							 * today also
							 */
							if (offset_from_today == 0) {
								int deep_sleep = (Integer
										.valueOf(DashBoardFragment.singleTon
												.getSleepTodayDeepHours()) * 60 + Integer
										.valueOf(DashBoardFragment.singleTon
												.getSleepTodayDeepMinutes()));
								int light_sleep = (Integer
										.valueOf(DashBoardFragment.singleTon
												.getSleepTodayLightHours()) * 60 + Integer
										.valueOf(DashBoardFragment.singleTon
												.getSleepTodayLightMinutes()));

								SleepData.MONTH_DEEP_SLEEP[i + 1] = deep_sleep;
								SleepData.MONTH_LIGHT_SLEEP[i + 1] = light_sleep;
								SleepData.MONTH_TOTAL_SLEEP[i + 1] = (deep_sleep + light_sleep);

								total_deep_sleep_minutes += deep_sleep;
								total_light_sleep_minutes += light_sleep;
								total_sleep_minutes += (deep_sleep + light_sleep);

								break;
							}
						} else {
							offset_from_today--;
						}
					} else {
						offset_from_today--;
					}
				}
			}
		} else {
			/**
			 * If be not current month, need to sum all value of all days from
			 * first day of month to last day of month
			 */
			int offset_from_today = 0;
			offset_from_today = calculateOffsetInMonth(0, today,
					first_day_of_picker_month_in_year, current_month,
					max_day_of_picker_month, picker_year, current_year);

			/**
			 * Loop get SUM of data from first day of month to last day of month
			 */
			int CALORIES = 0;
			float DISTANCE = 0;
			int STEPS = 0;
			for (int i = 0; i < max_day_of_picker_month; i++) {
				/**
				 * Run from 1th of picker month to last day of picker month
				 */
				if (offset_from_today > FitnessHistoryData.fitness_history_data.length) {
					offset_from_today--;
				} else {
					/**
					 * Check Invalid Data
					 */
					if (offset_from_today == FitnessHistoryData.fitness_history_data.length) {
						break;
					}

					if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
						if (FitnessHistoryData.fitness_history_data[offset_from_today]
								.getLocalDate() != null) {
							/**
							 * Some days don't have data, so need check data not
							 * exceed first day of month & last day of month
							 */
							Calendar mCalendar = Calendar.getInstance();
							mCalendar
									.setTime(FitnessHistoryData.fitness_history_data[offset_from_today]
											.getLocalDate());
							if (mCalendar.get(Calendar.MONTH) == picker_month) {
								/**
								 * Activities value
								 */
								// Current value
								CALORIES = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getCaloriesCurrent());
								DISTANCE = Float
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getDistanceCurrent());
								STEPS = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getStepsCurrent());

								// Add into array to draw on chart
								FitnessData.MONTH_CALORIES[i] = CALORIES;
								FitnessData.MONTH_DISTANCE[i] = DISTANCE;
								FitnessData.MONTH_STEPS[i] = STEPS;

								// Sum all item data value before show on Page
								SUM_CALORIES += CALORIES;
								SUM_DISTANCE += DISTANCE;
								SUM_STEPS += STEPS;

								offset_from_today--;
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					} else {
						offset_from_today--;
					}
				}
			}

			total_light_sleep_minutes = 0;
			total_deep_sleep_minutes = 0;
			total_sleep_minutes = 0;

			int light_sleep_minutes = 0;
			int deep_sleep_minutes = 0;
			int sleep_minutes = 0;

			/**
			 * If be not current month, need to sum all value of all days from
			 * first day of month to last day of month
			 */
			offset_from_today = 0;
			offset_from_today = calculateOffsetInMonth(0, today,
					first_day_of_picker_month_in_year, current_month,
					max_day_of_picker_month, picker_year, current_year);
			for (int i = 0; i < max_day_of_picker_month; i++) {
				/**
				 * Run from 1th of picker month to last day of picker month
				 */
				if (offset_from_today > SleepData.sleep_history_data.length) {
					offset_from_today--;
					continue;
				} else if (offset_from_today == SleepData.sleep_history_data.length) {
					break;
				} else {
					/**
					 * Check Invalid Data
					 */
					if (SleepData.sleep_history_data[offset_from_today] != null) {
						if (SleepData.sleep_history_data[offset_from_today]
								.getRecordDate() != null) {
							/**
							 * Some days don't have data, so need check data not
							 * exceed first day of month & last day of month
							 */
							Calendar mCalendar = Calendar.getInstance();
							mCalendar
									.setTime(SleepData.sleep_history_data[offset_from_today]
											.getRecordDate());
							if (mCalendar.get(Calendar.DAY_OF_MONTH) >= first_day_of_picker_month
									& mCalendar.get(Calendar.DAY_OF_MONTH) <= max_day_of_picker_month
									& mCalendar.get(Calendar.MONTH) == picker_month) {
								/**
								 * Sleep value
								 */
								light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getLightSleep();
								deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getDeepSleep();
								sleep_minutes = light_sleep_minutes
										+ deep_sleep_minutes;

								SleepData.MONTH_DEEP_SLEEP[i] = deep_sleep_minutes;
								SleepData.MONTH_LIGHT_SLEEP[i] = light_sleep_minutes;
								SleepData.MONTH_TOTAL_SLEEP[i] = sleep_minutes;

								// Sum all item data value before show on Page
								total_light_sleep_minutes += light_sleep_minutes;
								total_deep_sleep_minutes += deep_sleep_minutes;
								total_sleep_minutes += sleep_minutes;

								offset_from_today--;
							} else {
								offset_from_today--;
								continue;
							}
						} else {
							offset_from_today--;
							continue;
						}
					} else {
						offset_from_today--;
						continue;
					}
				}
			}
		}

		// Get values
		for (int i : FitnessData.YEAR_CALORIES) {
			SUM_CALORIES += i;
		}
		for (float i : FitnessData.YEAR_DISTANCE) {
			SUM_DISTANCE += i;
		}
		for (int i : FitnessData.YEAR_STEPS) {
			SUM_STEPS += i;
		}

		/**
		 * Save chart data for drawing chart
		 */
		saveChartDataMonth();

		/**
		 * Save activities current value into singleton
		 */
		saveActivitiesCurrentValueIntoSingleTon(SUM_CALORIES + "", SUM_DISTANCE
				+ "", SUM_STEPS + "");

		long calories_goal = Long.parseLong(DashBoardFragment.singleTon
				.getCaloriesGoalDefault()) * max_day_of_picker_month;
		float distance_goal = Float.valueOf(DashBoardFragment.singleTon
				.getDistanceGoalDefault()) * max_day_of_picker_month;
		long steps_goal = Long.parseLong(DashBoardFragment.singleTon
				.getStepsGoalDefault()) * max_day_of_picker_month;

		/**
		 * Save activities goal value into singleton
		 */
		saveActivitiesGoalValueIntoSingleTon(calories_goal + "", distance_goal
				+ "", steps_goal + "");

		/**
		 * Update on UI of page
		 */
		// Need refresh
		updateFitnessDataOnPage(getActivity(),
				DashBoardFragment.singleTon.getCaloriesCurrent(),
				DashBoardFragment.singleTon.getCaloriesGoal(),
				DashBoardFragment.singleTon.getDistanceCurrent(),
				DashBoardFragment.singleTon.getDistanceGoal(),
				DashBoardFragment.singleTon.getStepsCurrent(),
				DashBoardFragment.singleTon.getStepsGoal());

		/**
		 * Save goal values into single ton
		 */
		long sleep_goal = Long.parseLong(DashBoardFragment.singleTon
				.getSleepGoalDefault()) * max_day_of_picker_month;

		/**
		 * Save activities goal value into singleton
		 */
		saveSleepGoalValueIntoSingleTon(sleep_goal + "");

		/**
		 * Update data on UI of page
		 */
		updateSleepDataOnPage(getActivity(), total_sleep_minutes / 60 + "",
				total_sleep_minutes % 60 + "", total_deep_sleep_minutes / 60
						+ "", total_deep_sleep_minutes % 60 + "",
				total_light_sleep_minutes / 60 + "", total_light_sleep_minutes
						% 60 + "", DashBoardFragment.singleTon.getSleepGoal());
	}

	private void sumDataMonths() {
		// Clear old data before add new data
		clearOldData();

		/**
		 * Get current month info
		 */
		Calendar mCalendarCurrent = Calendar.getInstance();
		int today = mCalendarCurrent.get(Calendar.DAY_OF_YEAR);
		int today_check = mCalendarCurrent.get(Calendar.DAY_OF_MONTH);
		int current_month = mCalendarCurrent.get(Calendar.MONTH);
		int current_year = mCalendarCurrent.get(Calendar.YEAR);

		/**
		 * Find out the month and year of selection
		 */
		int picker_month = DashBoardFragment.singleTon
				.getPrevious3MonthsIndex(getActivity());
		int picker_year = Integer.valueOf(DashBoardFragment.singleTon
				.getCurrentYear());

		boolean is_december = false;

		/**
		 * Begin get fitness data & sleep data
		 */
		int SUM_CALORIES = 0;
		float SUM_DISTANCE = 0;
		int SUM_STEPS = 0;

		int total_deep_sleep_minutes = 0;
		int total_light_sleep_minutes = 0;
		int total_sleep_minutes = 0;

		/**
		 * Run from first month to last month
		 */
		int[] months = new int[3];
		for (int month_count = 0; month_count < 3; month_count++) {
			// Store index of current month view
			DashBoardFragment.singleTon.setCurrentMonthIndex(picker_month);

			mCalendarCurrent.set(Calendar.DATE, 1); // set to first day
			mCalendarCurrent.set(Calendar.MONTH, picker_month);
			mCalendarCurrent.set(Calendar.YEAR, picker_year);

			/**
			 * Add to array to calculate the Goal value
			 */
			months[month_count] = mCalendarCurrent
					.getActualMaximum(Calendar.DAY_OF_MONTH);

			int first_day_of_picker_month_in_year = mCalendarCurrent
					.get(Calendar.DAY_OF_YEAR);
			int first_day_of_picker_month = mCalendarCurrent
					.get(Calendar.DAY_OF_MONTH);
			int max_day_of_picker_month = mCalendarCurrent
					.getActualMaximum(Calendar.DAY_OF_MONTH);

			mCalendarCurrent.set(Calendar.DATE, max_day_of_picker_month);

			if ((current_month == picker_month)
					&& (current_year == picker_year)) {
				/**
				 * If being Current month
				 */
				// count number of days till today
				Calendar mCalendarNow = Calendar.getInstance();
				int days = mCalendarNow.get(Calendar.DAY_OF_MONTH);

				/**
				 * Fitness values
				 */
				// Sum calories, distance, steps
				int CALORIES = 0;
				float DISTANCE = 0;
				int STEPS = 0;

				int offset_from_today = 0;
				if (today_check == 1) {
					// Add data into array to draw on chart
					FitnessData.MONTHS_CALORIES[month_count] += Integer
							.valueOf(DashBoardFragment.singleTon
									.getCaloriesToday());
					FitnessData.MONTHS_DISTANCE[month_count] += Float
							.valueOf(DashBoardFragment.singleTon
									.getDistanceToday());
					FitnessData.MONTHS_STEPS[month_count] += Integer
							.valueOf(DashBoardFragment.singleTon
									.getStepsToday());

					// Sum all item data value before show on
					// Page
					SUM_CALORIES += Integer.valueOf(DashBoardFragment.singleTon
							.getCaloriesToday());
					SUM_DISTANCE += Float.valueOf(DashBoardFragment.singleTon
							.getDistanceToday());
					SUM_STEPS += Integer.valueOf(DashBoardFragment.singleTon
							.getStepsToday());
				} else {
					offset_from_today = days - 1;
					for (int i = 0; i < days; i++) {
						if (offset_from_today > FitnessHistoryData.fitness_history_data.length) {
							offset_from_today--;
							continue;
						} else {
							if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
								if (FitnessHistoryData.fitness_history_data[offset_from_today]
										.getLocalDate() != null) {
									/**
									 * Activities current value
									 */
									CALORIES = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getCaloriesCurrent());
									DISTANCE = Float
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getDistanceCurrent());
									STEPS = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getStepsCurrent());

									// Add data into array to draw on chart
									FitnessData.MONTHS_CALORIES[month_count] += CALORIES;
									FitnessData.MONTHS_DISTANCE[month_count] += DISTANCE;
									FitnessData.MONTHS_STEPS[month_count] += STEPS;

									// Sum data
									SUM_CALORIES += CALORIES;
									SUM_DISTANCE += DISTANCE;
									SUM_STEPS += STEPS;

									offset_from_today--;

									/**
									 * If it is today, need get data of today to
									 * add into Weekly view
									 */
									if (offset_from_today == 0) {
										// Add data into array to draw on chart
										FitnessData.MONTHS_CALORIES[month_count] += Integer
												.valueOf(DashBoardFragment.singleTon
														.getCaloriesToday());
										FitnessData.MONTHS_DISTANCE[month_count] += Float
												.valueOf(DashBoardFragment.singleTon
														.getDistanceToday());
										FitnessData.MONTHS_STEPS[month_count] += Integer
												.valueOf(DashBoardFragment.singleTon
														.getStepsToday());

										// Sum all item data value before show
										// on
										// Page
										SUM_CALORIES += Integer
												.valueOf(DashBoardFragment.singleTon
														.getCaloriesToday());
										SUM_DISTANCE += Float
												.valueOf(DashBoardFragment.singleTon
														.getDistanceToday());
										SUM_STEPS += Integer
												.valueOf(DashBoardFragment.singleTon
														.getStepsToday());
										break;
									}

								} else {
									offset_from_today--;
								}
							} else {
								offset_from_today--;
							}
						}
					}
				}

				/**
				 * Sleep value
				 */
				int light_sleep_minutes = 0;
				int deep_sleep_minutes = 0;
				int sleep_minutes = 0;

				if (today_check == 1) {
					int deep_sleep = (Integer
							.valueOf(DashBoardFragment.singleTon
									.getSleepTodayDeepHours()) * 60 + Integer
							.valueOf(DashBoardFragment.singleTon
									.getSleepTodayDeepMinutes()));
					int light_sleep = (Integer
							.valueOf(DashBoardFragment.singleTon
									.getSleepTodayLightHours()) * 60 + Integer
							.valueOf(DashBoardFragment.singleTon
									.getSleepTodayLightMinutes()));

					SleepData.MONTH_DEEP_SLEEP[month_count] += deep_sleep;
					SleepData.MONTH_LIGHT_SLEEP[month_count] += light_sleep;
					SleepData.MONTH_TOTAL_SLEEP[month_count] += (deep_sleep + light_sleep);

					total_deep_sleep_minutes += deep_sleep;
					total_light_sleep_minutes += light_sleep;
					total_sleep_minutes += (deep_sleep + light_sleep);
				} else {
					offset_from_today = days - 1;
					for (int i = 0; i < days; i++) {
						if (offset_from_today > SleepData.sleep_history_data.length) {
							offset_from_today--;
							continue;
						} else {
							if (SleepData.sleep_history_data[offset_from_today] != null) {
								if (SleepData.sleep_history_data[offset_from_today]
										.getRecordDate() != null) {

									Calendar mCalendarDayHasData = Calendar
											.getInstance();
									mCalendarDayHasData
											.setTime(SleepData.sleep_history_data[offset_from_today]
													.getRecordDate());
									int current_day = mCalendarDayHasData
											.get(Calendar.DAY_OF_MONTH);
									int current_month_has_data = mCalendarDayHasData
											.get(Calendar.MONTH);

									// Run from today to first day of month
									if (current_day >= first_day_of_picker_month
											& current_month_has_data == current_month) {
										light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
												.getLightSleep();
										deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
												.getDeepSleep();
										sleep_minutes = light_sleep_minutes
												+ deep_sleep_minutes;

										SleepData.MONTHS_LIGHT_SLEEP[month_count] += light_sleep_minutes;
										SleepData.MONTHS_DEEP_SLEEP[month_count] += deep_sleep_minutes;
										SleepData.MONTHS_TOTAL_SLEEP[month_count] += sleep_minutes;

										// Sum all item data value before show
										// on
										// Page
										total_light_sleep_minutes += light_sleep_minutes;
										total_deep_sleep_minutes += deep_sleep_minutes;
										total_sleep_minutes += sleep_minutes;

										offset_from_today--;

										/**
										 * Need plus total sleep, deep sleep,
										 * light sleep of today also
										 */
										if (offset_from_today == 0) {
											int deep_sleep = (Integer
													.valueOf(DashBoardFragment.singleTon
															.getSleepTodayDeepHours()) * 60 + Integer
													.valueOf(DashBoardFragment.singleTon
															.getSleepTodayDeepMinutes()));
											int light_sleep = (Integer
													.valueOf(DashBoardFragment.singleTon
															.getSleepTodayLightHours()) * 60 + Integer
													.valueOf(DashBoardFragment.singleTon
															.getSleepTodayLightMinutes()));

											SleepData.MONTH_DEEP_SLEEP[month_count] += deep_sleep;
											SleepData.MONTH_LIGHT_SLEEP[month_count] += light_sleep;
											SleepData.MONTH_TOTAL_SLEEP[month_count] += (deep_sleep + light_sleep);

											total_deep_sleep_minutes += deep_sleep;
											total_light_sleep_minutes += light_sleep;
											total_sleep_minutes += (deep_sleep + light_sleep);
											break;
										}
									} else {
										offset_from_today--;
									}
								} else {
									offset_from_today--;
								}
							} else {
								offset_from_today--;
							}
						}
					}
				}
			} else {
				/**
				 * If not being Current month
				 */

				/**
				 * Fitness values
				 */
				int CALORIES = 0;
				float DISTANCE = 0;
				int STEPS = 0;

				int offset_from_today = 0;
				offset_from_today = calculateOffsetInMonths(0, today,
						first_day_of_picker_month_in_year, picker_year,
						current_year);
				for (int i = 0; i < max_day_of_picker_month; i++) {
					if (offset_from_today > FitnessHistoryData.fitness_history_data.length - 1) {
						offset_from_today--;
						continue;
					} else {
						if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
							if (FitnessHistoryData.fitness_history_data[offset_from_today]
									.getLocalDate() != null) {
								/**
								 * Some days don't have data, so need check data
								 * not exceed first day of month & last day of
								 * month
								 */
								Calendar mCalendar = Calendar.getInstance();
								mCalendar
										.setTime(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getLocalDate());
								if (mCalendar.get(Calendar.MONTH) >= picker_month
										& mCalendar.get(Calendar.YEAR) == picker_year) {
									// get from Historical data
									// Call UI for updated
									CALORIES = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getCaloriesCurrent());
									DISTANCE = Float
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getDistanceCurrent());
									STEPS = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getStepsCurrent());

									FitnessData.MONTHS_CALORIES[month_count] += CALORIES;
									FitnessData.MONTHS_DISTANCE[month_count] += DISTANCE;
									FitnessData.MONTHS_STEPS[month_count] += STEPS;

									SUM_CALORIES += CALORIES;
									SUM_DISTANCE += DISTANCE;
									SUM_STEPS += STEPS;

									offset_from_today--;
								} else if (mCalendar.get(Calendar.MONTH) <= picker_month
										& mCalendar.get(Calendar.YEAR) > picker_year) {
									// get from Historical data
									// Call UI for updated
									CALORIES = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getCaloriesCurrent());
									DISTANCE = Float
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getDistanceCurrent());
									STEPS = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
													.getStepsCurrent());

									FitnessData.MONTHS_CALORIES[month_count] += CALORIES;
									FitnessData.MONTHS_DISTANCE[month_count] += DISTANCE;
									FitnessData.MONTHS_STEPS[month_count] += STEPS;

									SUM_CALORIES += CALORIES;
									SUM_DISTANCE += DISTANCE;
									SUM_STEPS += STEPS;

									offset_from_today--;
								} else {
									offset_from_today--;
									continue;
								}
							} else {
								offset_from_today--;
								continue;
							}
						} else {
							offset_from_today--;
							continue;
						}
					}
				}

				/**
				 * Sleep values
				 */
				int light_sleep_minutes = 0;
				int deep_sleep_minutes = 0;
				int sleep_minutes = 0;

				Calendar mCalendarDetermineDecember = Calendar.getInstance();
				mCalendarDetermineDecember.set(Calendar.YEAR, picker_year);
				mCalendarDetermineDecember.set(Calendar.DAY_OF_YEAR,
						first_day_of_picker_month_in_year);

				/**
				 * If passes December, should increase year
				 */
				if (is_december) {
					is_december = false;
					picker_year++;
				}

				/**
				 * If this is December, set boolean is true before increase year
				 */
				if (mCalendarDetermineDecember.get(Calendar.MONTH) == Calendar.DECEMBER) {
					is_december = true;
				}

				offset_from_today = 0;
				offset_from_today = calculateOffsetInMonths(0, today,
						first_day_of_picker_month_in_year, picker_year,
						current_year);

				for (int i = 0; i < max_day_of_picker_month; i++) {
					if (offset_from_today > SleepData.sleep_history_data.length - 1) {
						offset_from_today--;
						continue;
					} else {
						if (SleepData.sleep_history_data[offset_from_today] != null) {
							if (SleepData.sleep_history_data[offset_from_today]
									.getRecordDate() != null) {
								/**
								 * Some days don't have data, so need check data
								 * not exceed first day of month & last day of
								 * month
								 */
								Calendar mCalendar = Calendar.getInstance();
								mCalendar
										.setTime(SleepData.sleep_history_data[offset_from_today]
												.getRecordDate());

								deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getDeepSleep();
								light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getLightSleep();
								sleep_minutes = light_sleep_minutes
										+ deep_sleep_minutes;

								SleepData.MONTHS_LIGHT_SLEEP[month_count] += light_sleep_minutes;
								SleepData.MONTHS_DEEP_SLEEP[month_count] += deep_sleep_minutes;
								SleepData.MONTHS_TOTAL_SLEEP[month_count] += sleep_minutes;

								// Sum all item data value before show on
								// Page
								total_light_sleep_minutes += light_sleep_minutes;
								total_deep_sleep_minutes += deep_sleep_minutes;
								total_sleep_minutes += sleep_minutes;

								offset_from_today--;
							} else {
								offset_from_today--;
								continue;
							}
						} else {
							offset_from_today--;
							continue;
						}
					}
				}
			}
			picker_month++;
			if (picker_month >= 13)
				picker_month = 1;

			if (picker_month == 1)
				picker_year++;
		}

		/**
		 * Save chart data for drawing chart
		 */
		saveChartDataMonths();

		/**
		 * Save activities current value into single ton
		 */
		saveActivitiesCurrentValueIntoSingleTon(SUM_CALORIES + "", SUM_DISTANCE
				+ "", SUM_STEPS + "");

		// Get max days of months
		int sum_max_days_of_months = 0;
		for (int month_count = 0; month_count < 3; month_count++) {
			sum_max_days_of_months += months[month_count];
		}

		long calories_goal = Long.parseLong(DashBoardFragment.singleTon
				.getCaloriesGoalDefault()) * sum_max_days_of_months;
		float distance_goal = Float.valueOf(DashBoardFragment.singleTon
				.getDistanceGoalDefault()) * sum_max_days_of_months;
		long steps_goal = Long.parseLong(DashBoardFragment.singleTon
				.getStepsGoalDefault()) * sum_max_days_of_months;

		/**
		 * Save activities goal value into single ton
		 */
		saveActivitiesGoalValueIntoSingleTon(calories_goal + "", distance_goal
				+ "", steps_goal + "");

		/**
		 * Save goal values into single ton
		 */
		long sleep_goal = Long.parseLong(DashBoardFragment.singleTon
				.getSleepGoalDefault()) * sum_max_days_of_months;

		/**
		 * Save activities goal value into singleton
		 */
		saveSleepGoalValueIntoSingleTon(sleep_goal + "");

		/**
		 * Update UI on page
		 */
		// Need refresh
		updateFitnessDataOnPage(getActivity(),
				DashBoardFragment.singleTon.getCaloriesCurrent(),
				DashBoardFragment.singleTon.getCaloriesGoal(),
				DashBoardFragment.singleTon.getDistanceCurrent(),
				DashBoardFragment.singleTon.getDistanceGoal(),
				DashBoardFragment.singleTon.getStepsCurrent(),
				DashBoardFragment.singleTon.getStepsGoal());

		updateSleepDataOnPage(getActivity(), total_sleep_minutes / 60 + "",
				total_sleep_minutes % 60 + "", total_deep_sleep_minutes / 60
						+ "", total_deep_sleep_minutes % 60 + "",
				total_light_sleep_minutes / 60 + "", total_light_sleep_minutes
						% 60 + "", DashBoardFragment.singleTon.getSleepGoal());
	}

	private void sumDataWeek() {
		// Clear old data before add new data
		clearOldData();

		/**
		 * Get current Monday and Sunday info
		 */
		Date mDateMonday = DashBoardFragment.singleTon.getDateMonday();
		Date mDateSunday = DashBoardFragment.singleTon.getDateSunday();

		// Today
		Calendar mCalendarCurrent = Calendar.getInstance();
		int today = mCalendarCurrent.get(Calendar.DAY_OF_YEAR);

		// Monday time
		Calendar mCalendarMonday = Calendar.getInstance();
		mCalendarMonday.setTime(mDateMonday);

		// Sunday time
		Calendar mCalendarSunday = Calendar.getInstance();
		mCalendarSunday.setTime(mDateSunday);

		// Get last day of Last year
		Calendar mCalendarLastDayOfLastYear = Calendar.getInstance();
		mCalendarLastDayOfLastYear.set(Calendar.MONTH, Calendar.DECEMBER);
		mCalendarLastDayOfLastYear.set(Calendar.DATE, 31);
		mCalendarLastDayOfLastYear.set(Calendar.YEAR,
				mCalendarMonday.get(Calendar.YEAR));

		int last_day_of_last_year = mCalendarLastDayOfLastYear
				.get(Calendar.DAY_OF_YEAR);
		int monday = mCalendarMonday.get(Calendar.DAY_OF_YEAR);
		int sunday = mCalendarSunday.get(Calendar.DAY_OF_YEAR);

		/**
		 * Sleep info to show on page
		 */
		int total_deep_sleep_minutes = 0;
		int total_light_sleep_minutes = 0;
		int total_sleep_minutes = 0;

		int SUM_CALORIES = 0;
		float SUM_DISTANCE = 0;
		int SUM_STEPS = 0;

		/**
		 * Different year
		 */
		// Case : Monday and Sunday have different year
		if (mCalendarMonday.get(Calendar.YEAR) < mCalendarSunday
				.get(Calendar.YEAR)) {
			/**
			 * Fitness values
			 */
			int CALORIES = 0;
			float DISTANCE = 0;
			int STEPS = 0;

			int day_count_first_year = 0;
			int offset_from_today = (today) + (last_day_of_last_year - monday);
			for (int i = 0; i < last_day_of_last_year - monday; i++) {
				if (offset_from_today > FitnessHistoryData.fitness_history_data.length) {
					offset_from_today--;
				} else {
					if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
						if (FitnessHistoryData.fitness_history_data[offset_from_today]
								.getLocalDate() != null) {
							CALORIES = Integer
									.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
											.getCaloriesCurrent());
							DISTANCE = Float
									.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
											.getDistanceCurrent());
							STEPS = Integer
									.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
											.getStepsCurrent());

							FitnessData.WEEK_CALORIES[i] = CALORIES;
							FitnessData.WEEK_DISTANCE[i] = DISTANCE;
							FitnessData.WEEK_STEPS[i] = STEPS;

							// Sum all item data value before show on Page
							SUM_CALORIES += CALORIES;
							SUM_DISTANCE += DISTANCE;
							SUM_STEPS += STEPS;

							day_count_first_year++;
							offset_from_today--;

							// sum 1st Jan this year till today
							for (i = 0; i <= sunday; i++) {
								if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
									if (FitnessHistoryData.fitness_history_data[offset_from_today]
											.getLocalDate() != null) {
										CALORIES = Integer
												.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
														.getCaloriesCurrent());
										DISTANCE = Float
												.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
														.getDistanceCurrent());
										STEPS = Integer
												.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
														.getStepsCurrent());

										FitnessData.WEEK_CALORIES[day_count_first_year
												+ i] = CALORIES;
										FitnessData.WEEK_DISTANCE[day_count_first_year
												+ i] = DISTANCE;
										FitnessData.WEEK_STEPS[day_count_first_year
												+ i] = STEPS;

										// Sum all item data value before show
										// on
										// Page
										SUM_CALORIES += CALORIES;
										SUM_DISTANCE += DISTANCE;
										SUM_STEPS += STEPS;

										offset_from_today--;
									} else {
										offset_from_today--;
									}
								} else {
									offset_from_today--;
								}
							}
						} else {
							offset_from_today--;
						}
					} else {
						offset_from_today--;
					}
				}
			}

			/**
			 * Sleep values
			 */
			int light_sleep_minutes = 0;
			int deep_sleep_minutes = 0;
			int sleep_minutes = 0;

			day_count_first_year = 0;
			offset_from_today = (today) + (last_day_of_last_year - monday);
			for (int i = 0; i < last_day_of_last_year - monday; i++) {
				if (offset_from_today > SleepData.sleep_history_data.length) {
					offset_from_today--;
				} else {
					if (SleepData.sleep_history_data[offset_from_today] != null) {
						if (SleepData.sleep_history_data[offset_from_today]
								.getRecordDate() != null) {
							/**
							 * Sleep value
							 */
							light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
									.getLightSleep();
							deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
									.getDeepSleep();
							sleep_minutes = light_sleep_minutes
									+ deep_sleep_minutes;

							SleepData.WEEK_LIGHT_SLEEP[i] = light_sleep_minutes;
							SleepData.WEEK_DEEP_SLEEP[i] = deep_sleep_minutes;
							SleepData.WEEK_TOTAL_SLEEP[i] = sleep_minutes;

							// Sum all item data value before show on Page
							total_light_sleep_minutes += light_sleep_minutes;
							total_deep_sleep_minutes += deep_sleep_minutes;
							total_sleep_minutes += sleep_minutes;

							day_count_first_year++;
							offset_from_today--;

							// sum 1st Jan this year till today
							for (i = 0; i <= sunday; i++) {
								/**
								 * Sleep value
								 */
								if (SleepData.sleep_history_data[offset_from_today] != null) {
									if (SleepData.sleep_history_data[offset_from_today]
											.getRecordDate() != null) {
										light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
												.getLightSleep();
										deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
												.getDeepSleep();
										sleep_minutes = light_sleep_minutes
												+ deep_sleep_minutes;

										SleepData.WEEK_LIGHT_SLEEP[day_count_first_year
												+ i] = light_sleep_minutes;
										SleepData.WEEK_DEEP_SLEEP[day_count_first_year
												+ i] = deep_sleep_minutes;
										SleepData.WEEK_TOTAL_SLEEP[day_count_first_year
												+ i] = sleep_minutes;

										// Sum all item data value before show
										// on
										// Page
										total_light_sleep_minutes += light_sleep_minutes;
										total_deep_sleep_minutes += deep_sleep_minutes;
										total_sleep_minutes += sleep_minutes;

										offset_from_today--;
									} else {
										offset_from_today--;
									}
								} else {
									offset_from_today--;
								}
							}
						} else {
							offset_from_today--;
						}
					} else {
						offset_from_today--;
					}
				}
			}
		} else {
			/**
			 * Same year
			 */
			int historicalday_until_today = today - monday;
			if (historicalday_until_today >= 0 & historicalday_until_today < 7) {
				if (historicalday_until_today == 0) {
					// Fitness
					SUM_CALORIES += Integer.valueOf(DashBoardFragment.singleTon
							.getCaloriesToday());
					SUM_DISTANCE += Float.valueOf(DashBoardFragment.singleTon
							.getDistanceToday());
					SUM_STEPS += Integer.valueOf(DashBoardFragment.singleTon
							.getStepsToday());

					FitnessData.WEEK_CALORIES[0] += SUM_CALORIES;
					FitnessData.WEEK_DISTANCE[0] += SUM_DISTANCE;
					FitnessData.WEEK_STEPS[0] += SUM_STEPS;

					// Sleep
					total_deep_sleep_minutes += Integer
							.valueOf(DashBoardFragment.singleTon
									.getSleepTodayDeepMinutes());
					total_light_sleep_minutes += Integer
							.valueOf(DashBoardFragment.singleTon
									.getSleepTodayLightMinutes());
					total_sleep_minutes += Integer
							.valueOf(DashBoardFragment.singleTon
									.getSleepTodayTotal());

					SleepData.WEEK_DEEP_SLEEP[0] = total_deep_sleep_minutes;
					SleepData.WEEK_LIGHT_SLEEP[0] = total_light_sleep_minutes;
					SleepData.WEEK_TOTAL_SLEEP[0] = total_sleep_minutes;
				}

				/**
				 * If this is current week
				 */
				// need to add current day data as today is before Sunday
				/**
				 * Should use Index = 0, it is today, need its data to sum up of
				 * the week
				 */
				/**
				 * Fitness values
				 */
				int CALORIES = 0;
				float DISTANCE = 0;
				int STEPS = 0;

				int offset_from_today = today - monday;
				for (int i = 0; i < historicalday_until_today; i++) {
					if (offset_from_today > FitnessHistoryData.fitness_history_data.length) {
						offset_from_today--;
					} else {
						if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
							if (FitnessHistoryData.fitness_history_data[offset_from_today]
									.getLocalDate() != null) {
								/**
								 * Activities value
								 */
								CALORIES = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getCaloriesCurrent());
								DISTANCE = Float
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getDistanceCurrent());
								STEPS = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getStepsCurrent());

								FitnessData.WEEK_CALORIES[i] += CALORIES;
								FitnessData.WEEK_DISTANCE[i] += DISTANCE;
								FitnessData.WEEK_STEPS[i] += STEPS;

								SUM_CALORIES += CALORIES;
								SUM_DISTANCE += DISTANCE;
								SUM_STEPS += STEPS;

								offset_from_today--;
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}

						/**
						 * If it is today, need get data of today to add into
						 * Weekly view
						 */
						if (offset_from_today == 0) {
							if (i + 1 != 7) {
								FitnessData.WEEK_CALORIES[i + 1] += Integer
										.valueOf(DashBoardFragment.singleTon
												.getCaloriesToday());
								FitnessData.WEEK_DISTANCE[i + 1] += Float
										.valueOf(DashBoardFragment.singleTon
												.getDistanceToday());
								FitnessData.WEEK_STEPS[i + 1] += Integer
										.valueOf(DashBoardFragment.singleTon
												.getStepsToday());

								SUM_CALORIES += Integer
										.valueOf(DashBoardFragment.singleTon
												.getCaloriesToday());
								SUM_DISTANCE += Float
										.valueOf(DashBoardFragment.singleTon
												.getDistanceToday());
								SUM_STEPS += Integer
										.valueOf(DashBoardFragment.singleTon
												.getStepsToday());
								break;
							}
						}
					}
				}

				/**
				 * Sleep value
				 */
				int light_sleep_minutes = 0;
				int deep_sleep_minutes = 0;
				int sleep_minutes = 0;

				total_deep_sleep_minutes = 0;
				total_light_sleep_minutes = 0;
				total_sleep_minutes = 0;

				offset_from_today = today - monday;
				for (int i = 0; i < historicalday_until_today; i++) {
					if (offset_from_today > SleepData.sleep_history_data.length) {
						offset_from_today--;
					} else {
						if (SleepData.sleep_history_data[offset_from_today] != null) {
							if (SleepData.sleep_history_data[offset_from_today]
									.getRecordDate() != null) {
								light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getLightSleep();
								deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getDeepSleep();
								sleep_minutes = light_sleep_minutes
										+ deep_sleep_minutes;

								SleepData.WEEK_LIGHT_SLEEP[i] = light_sleep_minutes;
								SleepData.WEEK_DEEP_SLEEP[i] = deep_sleep_minutes;
								SleepData.WEEK_TOTAL_SLEEP[i] = sleep_minutes;

								total_deep_sleep_minutes += deep_sleep_minutes;
								total_light_sleep_minutes += light_sleep_minutes;
								total_sleep_minutes += sleep_minutes;

								offset_from_today--;

								/**
								 * Need plus total sleep, deep sleep, light
								 * sleep of today also
								 */
								if (offset_from_today == 0) {
									int deep_sleep = (Integer
											.valueOf(DashBoardFragment.singleTon
													.getSleepTodayDeepHours()) * 60 + Integer
											.valueOf(DashBoardFragment.singleTon
													.getSleepTodayDeepMinutes()));
									int light_sleep = (Integer
											.valueOf(DashBoardFragment.singleTon
													.getSleepTodayLightHours()) * 60 + Integer
											.valueOf(DashBoardFragment.singleTon
													.getSleepTodayLightMinutes()));

									if (i + 1 != 7) {
										SleepData.WEEK_LIGHT_SLEEP[i + 1] = deep_sleep;
										SleepData.WEEK_DEEP_SLEEP[i + 1] = light_sleep;
										SleepData.WEEK_TOTAL_SLEEP[i + 1] = (deep_sleep + light_sleep);

										total_deep_sleep_minutes += deep_sleep;
										total_light_sleep_minutes += light_sleep;
										total_sleep_minutes += (deep_sleep + light_sleep);
										break;
									}
								}
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					}
				}
			} else if ((monday < sunday)
					& (mCalendarMonday.get(Calendar.YEAR) == mCalendarSunday
							.get(Calendar.YEAR))) {
				/**
				 * If this is earlier week in same year
				 */
				// this week is earlier week, only need historical data
				/**
				 * Fitness values
				 */
				int CALORIES = 0;
				float DISTANCE = 0;
				int STEPS = 0;

				int offset_from_today = 0;
				if (today - monday > 0) {
					offset_from_today = today - monday;
				} else {
					offset_from_today = today
							+ (last_day_of_last_year - monday);
				}

				for (int i = 0; i < 7; i++) {
					if (offset_from_today > FitnessHistoryData.fitness_history_data.length) {
						offset_from_today--;
					} else {
						if (offset_from_today < 0) {
							break;
						}
						if (FitnessHistoryData.fitness_history_data[offset_from_today] != null) {
							if (FitnessHistoryData.fitness_history_data[offset_from_today]
									.getLocalDate() != null) {
								/**
								 * Activities value
								 */
								CALORIES = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getCaloriesCurrent());
								DISTANCE = Float
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getDistanceCurrent());
								STEPS = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today]
												.getStepsCurrent());

								FitnessData.WEEK_CALORIES[i] += CALORIES;
								FitnessData.WEEK_DISTANCE[i] += DISTANCE;
								FitnessData.WEEK_STEPS[i] += STEPS;

								SUM_CALORIES += CALORIES;
								SUM_DISTANCE += DISTANCE;
								SUM_STEPS += STEPS;

								offset_from_today--;
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					}
				}

				/**
				 * Sleep value
				 */
				int light_sleep_minutes = 0;
				int deep_sleep_minutes = 0;
				int sleep_minutes = 0;

				total_deep_sleep_minutes = 0;
				total_light_sleep_minutes = 0;
				total_sleep_minutes = 0;

				if (today - monday > 0) {
					offset_from_today = today - monday;
				} else {
					offset_from_today = today
							+ (last_day_of_last_year - monday);
				}

				for (int i = 0; i < 7; i++) {
					if (offset_from_today > SleepData.sleep_history_data.length) {
						offset_from_today--;
					} else {
						if (offset_from_today < 0) {
							break;
						}

						if (SleepData.sleep_history_data[offset_from_today] != null) {
							if (SleepData.sleep_history_data[offset_from_today]
									.getRecordDate() != null) {
								light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getLightSleep();
								deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getDeepSleep();
								sleep_minutes = light_sleep_minutes
										+ deep_sleep_minutes;

								SleepData.WEEK_LIGHT_SLEEP[i] = light_sleep_minutes;
								SleepData.WEEK_DEEP_SLEEP[i] = deep_sleep_minutes;
								SleepData.WEEK_TOTAL_SLEEP[i] = sleep_minutes;

								// Sum all item data value before show on Page
								total_light_sleep_minutes += light_sleep_minutes;
								total_deep_sleep_minutes += deep_sleep_minutes;
								total_sleep_minutes += sleep_minutes;

								offset_from_today--;
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					}
				}
			}
		}

		/**
		 * Save chart data into singleton
		 */
		saveChartDataWeek();

		/**
		 * Save activities current value into singleton
		 */
		saveActivitiesCurrentValueIntoSingleTon(SUM_CALORIES + "", SUM_DISTANCE
				+ "", SUM_STEPS + "");

		long calories_goal = Long.parseLong(DashBoardFragment.singleTon
				.getCaloriesGoalDefault()) * 7;
		float distance_goal = Float.valueOf(DashBoardFragment.singleTon
				.getDistanceGoalDefault()) * 7;
		long steps_goal = Long.parseLong(DashBoardFragment.singleTon
				.getStepsGoalDefault()) * 7;

		/**
		 * Save activities current value into singleton
		 */
		saveActivitiesGoalValueIntoSingleTon(calories_goal + "", distance_goal
				+ "", steps_goal + "");

		/**
		 * Save goal values into single ton
		 */
		long sleep_goal = Long.parseLong(DashBoardFragment.singleTon
				.getSleepGoalDefault()) * 7;

		/**
		 * Save activities goal value into singleton
		 */
		saveSleepGoalValueIntoSingleTon(sleep_goal + "");

		/**
		 * Update UI on page
		 */
		// Need refresh
		updateFitnessDataOnPage(getActivity(),
				DashBoardFragment.singleTon.getCaloriesCurrent(),
				DashBoardFragment.singleTon.getCaloriesGoal(),
				DashBoardFragment.singleTon.getDistanceCurrent(),
				DashBoardFragment.singleTon.getDistanceGoal(),
				DashBoardFragment.singleTon.getStepsCurrent(),
				DashBoardFragment.singleTon.getStepsGoal());

		updateSleepDataOnPage(getActivity(), total_sleep_minutes / 60 + "",
				total_sleep_minutes % 60 + "", total_deep_sleep_minutes / 60
						+ "", total_deep_sleep_minutes % 60 + "",
				total_light_sleep_minutes / 60 + "", total_light_sleep_minutes
						% 60 + "", DashBoardFragment.singleTon.getSleepGoal());
	}

	private void sumDataYear() {
		// Clear old data before add new data
		clearOldData();

		// Picker year info
		int picker_year = Integer.valueOf(DashBoardFragment.singleTon
				.getCurrentYear());

		Calendar mCalendarCurrent = Calendar.getInstance(); // today reference
		int today = mCalendarCurrent.get(Calendar.DAY_OF_YEAR);

		Calendar mCalendarDecemberOfLastYear = Calendar.getInstance();

		// first day in historical chart
		Calendar mCalendarLastYear = Calendar.getInstance();
		mCalendarLastYear.add(Calendar.DAY_OF_YEAR, -365);

		int SUM_CALORIES = 0;
		float SUM_DISTANCE = 0;
		int SUM_STEPS = 0;

		/**
		 * Sleep info to show on page
		 */
		int total_deep_sleep_minutes = 0;
		int total_light_sleep_minutes = 0;
		int total_sleep_minutes = 0;

		if (mCalendarCurrent.get(Calendar.YEAR) > picker_year) {
			/**
			 * If picker year is not be current year
			 */
			// last year is selected
			// find first day of data and sum until 31st December
			mCalendarDecemberOfLastYear.set(Calendar.DATE, 31); // set to last
			// day of year
			mCalendarDecemberOfLastYear.set(Calendar.MONTH, Calendar.DECEMBER);
			mCalendarDecemberOfLastYear.set(Calendar.YEAR, picker_year);

			int first_day_of_last_year = mCalendarLastYear
					.get(Calendar.DAY_OF_YEAR);
			int offset_of_last_year = mCalendarDecemberOfLastYear
					.get(Calendar.DAY_OF_YEAR) - first_day_of_last_year;

			// now starts from first day of historical data
			long current_year_ago_time = mCalendarLastYear.getTimeInMillis() / 1000;
			current_year_ago_time -= 31536000; // 365 days ago

			mCalendarLastYear.setTimeInMillis(current_year_ago_time * 1000);

			/**
			 * Fitness values
			 */
			int CALORIES = 0;
			float DISTANCE = 0;
			int STEPS = 0;

			// sum last year
			int offset_from_today = today + offset_of_last_year;
			for (int i = 0; i < offset_of_last_year; i++) {
				if (offset_from_today - 1 > FitnessHistoryData.fitness_history_data.length) {
					offset_from_today--;
				} else {
					if (FitnessHistoryData.fitness_history_data[offset_from_today - 1] != null) {
						if (FitnessHistoryData.fitness_history_data[offset_from_today - 1]
								.getLocalDate() != null) {
							/**
							 * Some days don't have data, so need check data not
							 * exceed first day of month & last day of month
							 */
							Calendar mCalendar = Calendar.getInstance();
							mCalendar
									.setTime(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
											.getLocalDate());
							if (mCalendar.get(Calendar.YEAR) == picker_year) {
								// get month and just add
								int month_index = mCalendar.get(Calendar.MONTH);

								/**
								 * Activities value
								 */
								CALORIES = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
												.getCaloriesCurrent());
								DISTANCE = Float
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
												.getDistanceCurrent());
								STEPS = Integer
										.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
												.getStepsCurrent());

								// Add into array to draw on chart
								FitnessData.YEAR_CALORIES[month_index] += CALORIES;
								FitnessData.YEAR_DISTANCE[month_index] += DISTANCE;
								FitnessData.YEAR_STEPS[month_index] += STEPS;

								// Sum data to show on page
								SUM_CALORIES += CALORIES;
								SUM_DISTANCE += DISTANCE;
								SUM_STEPS += STEPS;

								offset_from_today--;
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					} else {
						offset_from_today--;
					}
				}
			}

			/**
			 * Sleep value
			 */
			int light_sleep_minutes = 0;
			int deep_sleep_minutes = 0;
			int sleep_minutes = 0;

			int offset_last_of_day_has_valid_data = 0;
			for (int i = SleepData.sleep_history_data.length - 1; i > 0; i--) {
				if (SleepData.sleep_history_data[i] != null) {
					if (SleepData.sleep_history_data[i].getRecordDate() != null) {
						Calendar mCalendar = Calendar.getInstance();
						mCalendar.setTime(SleepData.sleep_history_data[i]
								.getRecordDate());

						offset_last_of_day_has_valid_data = i;
						break;
					}
				}
			}

			// sum last year
			offset_from_today = offset_last_of_day_has_valid_data;

			for (int i = 0; i < 195; i++) {
				if (offset_from_today > SleepData.sleep_history_data.length) {
					offset_from_today--;
					continue;
				} else {
					if (SleepData.sleep_history_data[offset_from_today] != null) {
						if (SleepData.sleep_history_data[offset_from_today]
								.getRecordDate() != null) {
							/**
							 * Some days don't have data, so need check data not
							 * exceed first day of month & last day of month
							 */
							Calendar mCalendar = Calendar.getInstance();
							mCalendar
									.setTime(SleepData.sleep_history_data[offset_from_today]
											.getRecordDate());

							if (mCalendar.get(Calendar.YEAR) == picker_year) {
								// get month and just add
								int month_index = mCalendar.get(Calendar.MONTH);

								light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getLightSleep();
								deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
										.getDeepSleep();
								sleep_minutes = light_sleep_minutes
										+ deep_sleep_minutes;

								SleepData.YEAR_LIGHT_SLEEP[month_index] += light_sleep_minutes;
								SleepData.YEAR_DEEP_SLEEP[month_index] += deep_sleep_minutes;
								SleepData.YEAR_TOTAL_SLEEP[month_index] += sleep_minutes;

								// Sum all item data value before show on Page
								total_light_sleep_minutes += light_sleep_minutes;
								total_deep_sleep_minutes += deep_sleep_minutes;
								total_sleep_minutes += sleep_minutes;

								offset_from_today--;
							} else {
								offset_from_today--;
							}
						}
					} else {
						offset_from_today--;
					}
				}
			}
		} else {
			/**
			 * If picker year is current year
			 */
			// Current year info
			Calendar mCalendarCurrentYear = Calendar.getInstance();
			mCalendarCurrentYear.set(Calendar.DATE, 1);
			mCalendarCurrentYear.set(Calendar.MONTH, Calendar.JANUARY);

			/**
			 * Fitness values
			 */
			int CALORIES = 0;
			float DISTANCE = 0;
			int STEPS = 0;

			int offset_from_today = 0;
			offset_from_today = today - 1;
			for (int i = 0; i < today; i++) {
				if (offset_from_today - 1 >= FitnessHistoryData.fitness_history_data.length) {
					offset_from_today--;
				} else {
					if (offset_from_today - 1 > 0) {
						if (FitnessHistoryData.fitness_history_data[offset_from_today - 1] != null) {
							if (FitnessHistoryData.fitness_history_data[offset_from_today - 1]
									.getLocalDate() != null) {
								/**
								 * Some days don't have data, so need check data
								 * not exceed first day of month & last day of
								 * month
								 */
								Calendar mCalendar = Calendar.getInstance();
								mCalendar
										.setTime(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
												.getLocalDate());
								if (mCalendar.get(Calendar.YEAR) == picker_year) {
									// mCalendarCurrentYear.add(Calendar.DATE,
									// 1);//
									// increment
									int month_index = mCalendar
											.get(Calendar.MONTH);

									/**
									 * Activities value
									 */
									CALORIES = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
													.getCaloriesCurrent());
									DISTANCE = Float
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
													.getDistanceCurrent());
									STEPS = Integer
											.valueOf(FitnessHistoryData.fitness_history_data[offset_from_today - 1]
													.getStepsCurrent());

									// Add into array to draw on chart
									FitnessData.YEAR_CALORIES[month_index] += CALORIES;
									FitnessData.YEAR_DISTANCE[month_index] += DISTANCE;
									FitnessData.YEAR_STEPS[month_index] += STEPS;

									// Sum data
									SUM_CALORIES += CALORIES;
									SUM_DISTANCE += DISTANCE;
									SUM_STEPS += STEPS;

									offset_from_today--;

									/**
									 * If it is today, need get data of today to
									 * add into Yearly view
									 */
									if (offset_from_today - 1 == 0) {
										// Add into array to draw on chart
										FitnessData.YEAR_CALORIES[month_index] += Integer
												.valueOf(DashBoardFragment.singleTon
														.getCaloriesToday());
										FitnessData.YEAR_DISTANCE[month_index] += Float
												.valueOf(DashBoardFragment.singleTon
														.getDistanceToday());
										FitnessData.YEAR_STEPS[month_index] += Integer
												.valueOf(DashBoardFragment.singleTon
														.getStepsToday());

										// Sum all item data value before show
										// on
										// Page
										SUM_CALORIES += Integer
												.valueOf(DashBoardFragment.singleTon
														.getCaloriesToday());
										SUM_DISTANCE += Float
												.valueOf(DashBoardFragment.singleTon
														.getDistanceToday());
										SUM_STEPS += Integer
												.valueOf(DashBoardFragment.singleTon
														.getStepsToday());
										break;
									}
								} else {
									offset_from_today--;
								}
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					}
				}
			}

			/**
			 * Sleep value
			 */
			int light_sleep_minutes = 0;
			int deep_sleep_minutes = 0;
			int sleep_minutes = 0;

			/**
			 * If be not current month, need to sum all value of all days from
			 * first day of month to last day of month
			 */
			offset_from_today = today - 1;
			for (int i = 0; i < today; i++) {
				if (offset_from_today >= SleepData.sleep_history_data.length) {
					offset_from_today--;
					continue;
				} else {
					if (offset_from_today > 0) {
						if (SleepData.sleep_history_data[offset_from_today] != null) {
							if (SleepData.sleep_history_data[offset_from_today]
									.getRecordDate() != null) {
								/**
								 * Some days don't have data, so need check data
								 * not exceed first day of month & last day of
								 * month
								 */
								Calendar mCalendar = Calendar.getInstance();
								mCalendar.set(Calendar.DATE, 1);
								mCalendar.set(Calendar.MONTH, Calendar.JANUARY);
								mCalendar
										.setTime(SleepData.sleep_history_data[offset_from_today]
												.getRecordDate());
								if (mCalendar.get(Calendar.YEAR) == picker_year) {
									int month_index = mCalendar
											.get(Calendar.MONTH);

									light_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
											.getLightSleep();
									deep_sleep_minutes = SleepData.sleep_history_data[offset_from_today]
											.getDeepSleep();
									sleep_minutes = light_sleep_minutes
											+ deep_sleep_minutes;

									SleepData.YEAR_LIGHT_SLEEP[month_index] += light_sleep_minutes;
									SleepData.YEAR_DEEP_SLEEP[month_index] += deep_sleep_minutes;
									SleepData.YEAR_TOTAL_SLEEP[month_index] += sleep_minutes;

									// Sum all item data value before show on
									// Page
									total_light_sleep_minutes += light_sleep_minutes;
									total_deep_sleep_minutes += deep_sleep_minutes;
									total_sleep_minutes += sleep_minutes;

									offset_from_today--;

									/**
									 * Need plus total sleep, deep sleep, light
									 * sleep of today also
									 */
									if (offset_from_today == 0) {
										int deep_sleep = (Integer
												.valueOf(DashBoardFragment.singleTon
														.getSleepTodayDeepHours()) * 60 + Integer
												.valueOf(DashBoardFragment.singleTon
														.getSleepTodayDeepMinutes()));
										int light_sleep = (Integer
												.valueOf(DashBoardFragment.singleTon
														.getSleepTodayLightHours()) * 60 + Integer
												.valueOf(DashBoardFragment.singleTon
														.getSleepTodayLightMinutes()));

										SleepData.YEAR_DEEP_SLEEP[month_index] += deep_sleep;
										SleepData.YEAR_LIGHT_SLEEP[month_index] += light_sleep;
										SleepData.YEAR_TOTAL_SLEEP[month_index] += (deep_sleep + light_sleep);

										total_deep_sleep_minutes += deep_sleep;
										total_light_sleep_minutes += light_sleep;
										total_sleep_minutes += (deep_sleep + light_sleep);
										break;
									}
								} else {
									offset_from_today--;
								}
							} else {
								offset_from_today--;
							}
						} else {
							offset_from_today--;
						}
					}
				}
			}
		}

		/**
		 * Save chart data value into singleton to draw on chart
		 */
		saveChartDataYear();

		/**
		 * Save activities current value into single ton
		 */
		saveActivitiesCurrentValueIntoSingleTon(SUM_CALORIES + "", SUM_DISTANCE
				+ "", SUM_STEPS + "");

		// Get days of year
		Calendar mCalendarGoal = Calendar.getInstance();
		mCalendarGoal.set(Calendar.YEAR, picker_year);
		int max_days_of_year = mCalendarGoal
				.getActualMaximum(Calendar.DAY_OF_YEAR);

		long calories_goal = Long.parseLong(DashBoardFragment.singleTon
				.getCaloriesGoalDefault()) * max_days_of_year;
		float distance_goal = Float.valueOf(DashBoardFragment.singleTon
				.getDistanceGoalDefault()) * max_days_of_year;
		long steps_goal = Long.parseLong(DashBoardFragment.singleTon
				.getStepsGoalDefault()) * max_days_of_year;

		/**
		 * Save activities goal value into single ton
		 */
		saveActivitiesGoalValueIntoSingleTon(calories_goal + "", distance_goal
				+ "", steps_goal + "");

		/**
		 * Save goal values into single ton
		 */
		long sleep_goal = Long.parseLong(DashBoardFragment.singleTon
				.getSleepGoalDefault()) * max_days_of_year;

		/**
		 * Save activities goal value into singleton
		 */
		saveSleepGoalValueIntoSingleTon(sleep_goal + "");

		/**
		 * Update on UI page
		 */
		// Need refresh
		updateFitnessDataOnPage(getActivity(),
				DashBoardFragment.singleTon.getCaloriesCurrent(),
				DashBoardFragment.singleTon.getCaloriesGoal(),
				DashBoardFragment.singleTon.getDistanceCurrent(),
				DashBoardFragment.singleTon.getDistanceGoal(),
				DashBoardFragment.singleTon.getStepsCurrent(),
				DashBoardFragment.singleTon.getStepsGoal());

		updateSleepDataOnPage(getActivity(), total_sleep_minutes / 60 + "",
				total_sleep_minutes % 60 + "", total_deep_sleep_minutes / 60
						+ "", total_deep_sleep_minutes % 60 + "",
				total_light_sleep_minutes / 60 + "", total_light_sleep_minutes
						% 60 + "", DashBoardFragment.singleTon.getSleepGoal());
	}

	private void transferToDashBoardDetailPage(Fragment mFm) {
		// Show Calories fragment in here
		getFragmentManager().beginTransaction()
				.replace(R.id.fr_content_activity_fitness, mFm)
				.addToBackStack(null).commitAllowingStateLoss();
	}

	public static void updateFitnessDataOnPage(Context context,
			String calories_current, String calories_goal,
			String distance_current, String distance_goal,
			String steps_current, String steps_goal) {
		saveActivitiesCurrentValueIntoSingleTon(calories_current,
				distance_current, steps_current);
		saveActivitiesGoalValueIntoSingleTon(calories_goal, distance_goal,
				steps_goal);

		/**
		 * Show Activities data on UI
		 */
		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			mTvCaloriesTitle
					.setText(Utils.showIntergerFormat(calories_current));
			mTvDistanceTitle.setText(Utils.getFloatFormat(
					Float.valueOf(distance_current)));
			mTvStepsTitle.setText(Utils.showIntergerFormat(steps_current));

			mTvCaloriesContent.setText(Utils.showIntergerFormat(calories_goal
					+ ""));
			mTvDistanceContent.setText(distance_goal + "");
			mTvStepsContent.setText(Utils.showIntergerFormat(steps_goal + ""));

			updateFitnessValueOnCircularProgres();
		} else {
			// Need refresh chart view also
			switch (mViewPager.getCurrentItem()) {
			case Position.CALORIES:
				DashBoardCaloriesSimpleFragment.showData(context,
						calories_current, singleTon.getCaloriesGoal());
				break;
			case Position.DISTANCE:
				DashBoardDistanceSimpleFragment.showData(context,
						distance_current, singleTon.getDistanceGoal());
				break;
			case Position.STEPS:
				DashBoardStepsSimpleFragment.showData(context, steps_current,
						singleTon.getStepsGoal());
				break;
			default:
				break;
			}
		}
	}

	public static void updateSleepDataOnPage(Context context,
			String total_sleep_hours, String total_sleep_minutes,
			String deep_sleep_hours, String deep_sleep_minutes,
			String light_sleep_hours, String light_sleep_minutes,
			String sleep_hours_goal) {
		saveSleepCurrentValueIntoSingleTon(total_sleep_hours,
				total_sleep_minutes);
		saveSleepGoalValueIntoSingleTon(sleep_hours_goal);

		// Save into single ton : Deep Sleep, Light Sleep, Total Sleep
		DashBoardFragment.singleTon.setDeepSleepHours(deep_sleep_hours);
		DashBoardFragment.singleTon.setDeepSleepMinutes(deep_sleep_minutes);

		DashBoardFragment.singleTon.setLightSleepHours(light_sleep_hours);
		DashBoardFragment.singleTon.setLightSleepMinutes(light_sleep_minutes);

		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			/**
			 * Correct the time
			 */
			mTvSleepHoursContent.setText(sleep_hours_goal + "h");
			mTvSleepHoursTitle.setText(Utils.getCorrectHours(
					Integer.valueOf(total_sleep_hours),
					Integer.valueOf(total_sleep_minutes))[0]
					+ "");
			mTvSleepMinutesTitle.setText(Utils.getCorrectHours(
					Integer.valueOf(total_sleep_hours),
					Integer.valueOf(total_sleep_minutes))[1]
					+ "");

			updateSleepValueOnCircularProgres();
		} else {
			// Need refresh chart view also
			switch (mViewPager.getCurrentItem()) {
			case Position.SLEEP:
				DashBoardSleepSimpleFragment.showDataOnPage(context);
				break;
			default:
				break;
			}
		}
	}

	private static void updateFitnessValueOnCircularProgres() {
		// Calories
		if (Integer.valueOf(singleTon.getCaloriesGoal().trim()) != 0) {
			float value = Float.valueOf(singleTon.getCaloriesCurrent())
					/ Float.valueOf(singleTon.getCaloriesGoal().trim());
			if (100 * value > 1000) {
				holoCircularPbCalories.setProgress(1);
			} else {
				holoCircularPbCalories.setProgress(value);
			}
		} else if (Integer.valueOf(singleTon.getCaloriesGoal().trim()) == 0) {
			/**
			 * If Goal value was set = 0, set 1000%
			 */
			holoCircularPbCalories.setProgress(1);
		}

		// Distance
		if (Float.valueOf(singleTon.getDistanceGoal().trim()) != 0) {
			float value = Float.valueOf(singleTon.getDistanceCurrent())
					/ Float.valueOf(singleTon.getDistanceGoal().trim());
			if (100 * value > 1000) {
				holoCircularPbDistance.setProgress(1);
			} else {
				holoCircularPbDistance.setProgress(value);
			}
		} else if (Float.valueOf(singleTon.getDistanceGoal().trim()) == 0) {
			/**
			 * If Goal value was set = 0, set 1000%
			 */
			holoCircularPbDistance.setProgress(1);
		}

		// Steps
		if (Integer.valueOf(singleTon.getStepsGoal().trim()) != 0) {
			float value = Float.valueOf(singleTon.getStepsCurrent())
					/ Float.valueOf(singleTon.getStepsGoal().trim());
			if (100 * value > 1000) {
				holoCircularPbSteps.setProgress(1);
			} else {
				holoCircularPbSteps.setProgress(value);
			}
		} else if (Integer.valueOf(singleTon.getStepsGoal().trim()) == 0) {
			/**
			 * If Goal value was set = 0, set 1000%
			 */
			holoCircularPbSteps.setProgress(1);
		}
	}

	private static void updateSleepValueOnCircularProgres() {
		// Sleep
		if (Integer.valueOf(singleTon.getSleepGoal().trim()) != 0) {
			float value = Float.valueOf(Integer
					.valueOf(DashBoardFragment.singleTon.getTotalSleepHours())
					* 60
					+ Integer.valueOf(DashBoardFragment.singleTon
							.getTotalSleepMinutes()))
					/ Float.valueOf(Integer.valueOf(DashBoardFragment.singleTon
							.getSleepGoal()) * 60);
			if (100 * value > 1000) {
				holoCircularPbSleep.setProgress(1);
			} else {
				holoCircularPbSleep.setProgress(value);
			}
		} else if (Integer.valueOf(singleTon.getSleepGoal().trim()) == 0) {
			/**
			 * If Goal value was set = 0, set 1000%
			 */
			holoCircularPbSleep.setProgress(1);
		}
	}
}